searchsanjaysharma
asked on
How to store resultset data in jtable.
We don't know the number of rows and columns returned. How to store it in Jtable.
How to modify the following code.
import java.sql.*;
import java.swing.*;
import java.awt.*;
class sqlselect
{
public static void main(String args[]) throws Exception
{
int rno;
String sname,scity;
Connection conn;
Statement stmt;
ResultSet rs;
//initialize and load the JDBC-ODBC Driver
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
//to get a datbase connection
conn=DriverManager.getConn ection("jd bc:odbc:sc hooldsn"," ","");
//to create a statement object to execute SQL statement
stmt=conn.createStatement( );
//Sends the sql statemtent to the DBMS & exec
rs=stmt.executeQuery("sele ct * from student");
JFrame frame = new JFrame();
frame.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
Object rowData[][] = {
{ "Row1-Column1", "Row1-Column2", "Row1-Column3" },
{ "Row2-Column1", "Row2-Column2", "Row2-Column3" }
};
Object columnNames[] = { "Column One", "Column Two", "Column Three" };
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
How to modify the following code.
import java.sql.*;
import java.swing.*;
import java.awt.*;
class sqlselect
{
public static void main(String args[]) throws Exception
{
int rno;
String sname,scity;
Connection conn;
Statement stmt;
ResultSet rs;
//initialize and load the JDBC-ODBC Driver
Class.forName("sun.jdbc.od
//to get a datbase connection
conn=DriverManager.getConn
//to create a statement object to execute SQL statement
stmt=conn.createStatement(
//Sends the sql statemtent to the DBMS & exec
rs=stmt.executeQuery("sele
JFrame frame = new JFrame();
frame.setDefaultCloseOpera
Object rowData[][] = {
{ "Row1-Column1", "Row1-Column2", "Row1-Column3" },
{ "Row2-Column1", "Row2-Column2", "Row2-Column3" }
};
Object columnNames[] = { "Column One", "Column Two", "Column Three" };
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:)
ASKER