HOw to load data into a jtable
I have an application that, i want to load data from a database into a jtable. I have discovered that, i can't dynamically load the values from the database into the table. I need help to be able to load the values from the database and change the table in relation to the changes in the database.
Please help.
[324 byte] By [
Morrizlea] at [2007-11-27 4:00:44]

# 1
use resultset.getMetaDate() to get information about the db table column = JTable column.
make a select query and update the JTable component with the data you got.
JTable
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
JDBC
http://java.sun.com/docs/books/tutorial/jdbc/index.html
Good luck
# 2
Hallo,
the best way is to use JTableModel interface - to create a constructor like that:
public class ResultSetTableModel implements TableModel {
ResultSet results; // The ResultSet to interpret
ResultSetTableModel(ResultSet res) throws SQLException {
this.results = res;// Save the results
....
public Object getValueAt(int row, int column) {
try {
results.absolute(row+1);// Go to the specified row
Object o = results.getObject(column+1); // Get value of the column
if (o == null) return null;
else return o.toString();// Convert it to a string
} catch (SQLException e) { return e.toString(); }
}
etc..
Hope it'll help a bit
good luck
Miroslav