mausam
Posts: 243
Nickname: mausam
Registered: Sep, 2003
|
|
Re: Populate JTable
|
Posted: Jan 19, 2004 5:25 AM
|
|
// TableModel definition
String[] tableColumnsName = {"col 1","col 2","col 3"};
DefaultTableModel aModel = (DefaultTableModel) aTable.getModel();
aModel.setColumnIdentifiers(tableColumnsName);
// the query
ResultSet rs = statement.executeQuery("select col1,col2,col3 from mytable");
// Loop through the ResultSet and transfer in the Model
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int colNo = rsmd.getColumnCount();
while(rs.next()){
Object[] objects = new Object[colNo];
// tanks to umit ozkan for the bug fix!
for(int i=0;i<colNo;i++){
objects[i]=rs.getObject(i+1);
}
aModel.addRow(objects);
}
aTable.setModel(aModel);
Sorry my last post was for some other question
|
|