I want to create the JTable run time . and it should bind the data from the table of the database.
For Example i want to display the list of all employees from empmaster table and the row and columns should be generated dynamically according to the rows and columns in table empmaster.for static table creation i m using the following code.
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from empmaster");
ResultSet rs1=st.executeQuery("select * from empmaster");
String title[]={"empcode","empname"};
int c=0;
while(rs.next())
{
c++;
}
Object obj[][]= new Object[c][2];
JTable table= new JTable(obj,title);
JScrollPane jsp= new JScrollPane(table);
add(jsp);
int i=0;
while(rs1.next())
{
table.setValueAt(rs.getString("empcode"),i,0);
table.setValueAt(rs.getString("empname"),i,1);
}
}
catch(Exception ex
{
ex.printStackTrace();
}
Use this to create
TableModeland just set the model to existingJTable.