Link to home
Start Free TrialLog in
Avatar of searchsanjaysharma
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.odbc.JdbcOdbcDriver");
            
            //to get a datbase connection
            conn=DriverManager.getConnection("jdbc:odbc:schooldsn","","");
      
            //to create a statement object to execute SQL statement
            stmt=conn.createStatement();
            //Sends the sql statemtent to the DBMS & exec
              rs=stmt.executeQuery("select * from student");
         
          JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.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);
     }
}
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of searchsanjaysharma
searchsanjaysharma

ASKER

ok
:)