Reputation: 103
I'll try to explain as good as I possibly can. I'm a noob and have been trying to fix this for hours now...
I have made a Java application that shows data from a Database that it is connected to. (ODBC/Microsoft SQL).
When I made the GUI for the application I decided to show the data in a jList.
I then realized that the information is too much and that I need column names etc (therefore an jTable)
What I want to do is to CHANGE so that the data shows up in a jTable instead of the jList. But I have no clue how to do it.
This is what the code looks like for now:
GUI
private void jComboBox1_actionPerformed(ActionEvent e)throws SQLException {
String selected = jComboBox1.getSelectedItem().toString();
String[]tmp = conn.showTable1(selected);
jList1.setListData(tmp);
}
Controller
public String[] showTable1(String table)throws SQLException {
ArrayList<String> list1 = dal.showTable1(table);
return list1.toArray(new String[list1.size()]);
}
Data Access Layer
public ArrayList<String> showTable1(String table)throws SQLException {
ArrayList<String> list1 = new ArrayList<String>();
Statement stmt = con.createStatement();
String query1 = "select * from [" +table+"]";
System.out.println(query1);
ResultSet rset = stmt.executeQuery(query1);
ResultSetMetaData rsmd = rset.getMetaData();
while (rset.next()) {
String result = "";
for(int i = 1; i < rsmd.getColumnCount(); i++) {
result += rset.getString(i) + "\t" + "\t";
//
}
list1.add(result);
}
return list1;
}
Please help!
Kind Regards, Chris
Upvotes: 1
Views: 644
Reputation: 324197
See Table From Database for a couple of simple suggestions:
ResultSetTableModel
.TableFromDatabase Example
code Upvotes: 2