I am trying to populate a Netbeans GUI-builder jTable using my Derby database data.
I am using the following code, in my Account.java class:
public DefaultTableModel getData() {
try {
String stmt = "SELECT * FROM APP.DATAVAULT";
PreparedStatement ps = Main.getPreparedStatement(stmt);
ResultSet rs = ps.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
Vector columns = new Vector(columnCount);
//store column names
for (int i = 1; i <= columnCount; i++) {
columns.add(md.getColumnName(i));
}
Vector data = new Vector();
Vector row;
while (rs.next()) {
row = new Vector(columnCount);
for (int i = 1; i <= columnCount; i++) {
row.add(rs.getString(i));
}
data.add(row);
//Debugging
}
// List.setModel(tableModel);
ps.close();
rs.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
DefaultTableModel tableModel = new DefaultTableModel(data, columns);
return tableModel;
}
Ideally, I want to be able to return the tableModel with the parameters data and columns inside, as I understand doing this method within my GUI is bad practice. All tutorials online do not show how to send the data across to another class, they just do the database code within the GUI classes.
I have an error where it cannot see data and columns because they are declared and used in an unreachable part of my method. After I've done this, I need to find a way of getting this across to my GUI class and setting the model for my jTable which was made by Netbeans GUI builder.
I have been searching this website for answers and I have tried many solutions. However, I never seem to get anything to work due to the way I have coded my system. I have also tried other websites such as:
http://tips4java.wordpress.com/2009/03/12/table-from-database/
http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans < this would have been ideal but it did not work. I followed it to a tee!
and have looked at the Javadocs for jTable, DefaultTableModel and ResultSetTableModel - I have by no means not tried doing this myself by learning etc...
How can I go about doing this with the way I have modelled my system? Also, anyway of fixing my method or should I scrap it altogether?