Generating JTable from database table

package process;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
/**
 * This class create JTable from Database table.
 * User program needs to specify database connection and corresponding atable name.
 * @author Hemraj
 */
public class TableToJTable{
    //private String table;
    private Connection con;
    public TableToJTable(Connection con){
        this.con=con;
    }
    /**
     * This method return JTable object created from Database table having same data asn structure
     * as in original table into database.
     * @param table Name of the database table to be coverted to JTable
     * @return JTable object that consist of data and structure of Database table
     * @throws java.lang.Exception Original object is deferent, e.i either SQLException or NullPointerException
     */
    public JTable getTable(String table)throws Exception{
        JTable t1=new JTable();
        DefaultTableModel dm=new DefaultTableModel();
        Statement st=con.createStatement();   
        ResultSet rs=st.executeQuery("select * from "+table);
        ResultSetMetaData rsmd=rs.getMetaData();
        //Coding to get columns-
        int cols=rsmd.getColumnCount();
        String c[]=new String[cols];
        for(int i=0;i<cols;i++){
            c[i]=rsmd.getColumnName(i+1);
            dm.addColumn(c[i]);
        }
        //get data from rows
        Object row[]=new Object[cols];
        while(rs.next()){
             for(int i=0;i<cols;i++){
                    row[i]=rs.getString(i+1);
                }
            dm.addRow(row);
        }
        t1.setModel(dm);
        con.close();
        return t1;
    }
    /**
     * This method return JTable object created from Database table having selected data and structure
     * as in original table into database.
     * @param table Name of the database table to be coverted to JTable
     * @param query Select query to specify selected columns and data to extracted from database table
     * @return JTable object that consist of selected data and structure of Database table
     * @throws java.lang.Exception Original object is deferent, e.i either SQLException or NullPointerException
     */
    public JTable getTable(String table,String query)throws Exception{
        JTable t1=new JTable();
        DefaultTableModel dm=new DefaultTableModel();
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery(query);
        ResultSetMetaData rsmd=rs.getMetaData();
        //Coding to get columns-
        int cols=rsmd.getColumnCount();
        String c[]=new String[cols];
        for(int i=0;i<cols;i++){
            c[i]=rsmd.getColumnName(i+1);
            dm.addColumn(c[i]);
        }


        //get data from rows
        Object row[]=new Object[cols];
        while(rs.next()){
             for(int i=0;i<cols;i++){
                    row[i]=rs.getString(i+1);
                }
            dm.addRow(row);
        }
        t1.setModel(dm);
        con.close();
        return t1;
    }


    public static void main(String ar[])throws Exception{
        JFrame f=new JFrame("Title");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:mydsn");
        process.TableToJTable obj = new process.TableToJTable(con);
        JScrollPane sp=new JScrollPane(obj.getTable("user"));
        f.add(sp);
        f.setBounds(200,200,400,300);
        f.setVisible(true);       
    }
}





8 comments:

testing said...

love u..will try this now :)

Anonymous said...

JUST wonderful....


many thanks

Anonymous said...

i can compile sucessfully but there is a error on run time

plz tell me is there is another packge of process

Hemraj said...

Have you created database to be added in datasource with DSN name "mydsn". I think database connection is not proper. Please copy and paste error message here.

Narendra Tiwary said...

Hi Hemraj,
I have tried this code, it compiles correctly but when trying to run it asks for "Package" which you have mentioned in very first line of this program. Kindly see the exact error.
"Error: Could not find or load main class process.TableToJTable
Press any key to continue . . ."

Narendra Tiwary said...

Hello Hemraj,
I have tried your code it gives no error at compiling level but at run time it throws an error and keep asking for Package Process which you have mentioned in your program at very first line. Request you to kindly see the error message.

Error: Could not find or load main class process.TableToJTable
Press any key to continue . . .

4uandmestudio.com said...

I tried this but I get this
"The method isWritable(int) in the type ResultSetMetaData is not applicable for the arguments (String)"

4uandmestudio.com said...

I have some problems when i implement this code. This uís the error I get
"The method isWritable(int) in the type ResultSetMetaData is not applicable for the arguments (String)"

Popular Posts