I have database set up on phpMyAdmin. I get access to it by using jdbc. And here goes my question: How can I fill table with data ?
Before I used to only use JFrame, so I got this code:
public void loadData(JTable table)
{
try{
Statement st = conn.createStatement();
st.executeQuery("SELECT Title, Last_name, Publisher " +
"FROM order " +
"");
ResultSet r = st.getResultSet();
ResultSetMetaData meta = r.getMetaData();
int cols = meta.getColumnCount();
String[] headers = new String[cols]
;
for(int i=0;i<cols;i++) headers[i]
= meta.getColumnName(i+1);
DefaultTableModel model = (DefaultTableModel) table.getModel();
while(model.getRowCount() > 0)
model.removeRow(0);
model.setColumnIdentifiers(headers);
while(r.next())
{
String[] row = new String[cols]
;
for(int i=0;i<cols;i++)
row[i]
= r.getString(i+1);
model.addRow(row);
}
table.setModel(model);
}
catch (Exception e) {e.printStackTrace(); }
}
What about using a BeanItemContainer?
You simply create beans, put them inside the container and set that container to the table by using the ‘setContainerDataSource’ method.
That’s all…
In addition, you might try SQLContainer, but I have no experience with it…