I have been given an assignment to create a database with eight different tables and then create Java code that allows me to access the tables and the data within them in various ways in a JFrame. I had no issue creating the database and accessing it, however I am having issues getting the data to appear.
The way I have been asked to do this is to have a JComboBox that contains all of the Table Names, and then the names of the columns within that table should appear within a JList. I don't know how to get access to the names of the columns once I have the table selected in the JComboBox.
Here is a portion of my code:
ResultSet rsTables = dbMetaData.getTables(null, null, null,
new String[] {"TABLE"});
// Store table names into a string array
int i = 0;
String[] tblNames = new String[8];
while (rsTables.next()) {
tblNames[i] = rsTables.getString("TABLE_NAME");
i += 1;
}
jcbTblNames = new JComboBox(tblNames);
jcbTblNames.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String selectedTbl = (String) (jcbTblNames.getSelectedItem());
}
});
This is the part of my code where I call upon the database to get the names of the tables and then have them put into a JComboBox. After that I added the ItemListener that changes based on which Table the user selects.
I have no idea how to take this selected Table and then create a JList of the columns. I'm assuming it has something to do with the following:
ResultSet resultSet = stmt.executeQuery("select * from " + selectedTbl);
String[] colNames = new String[10];
ResultSetMetaData rsMetaData = resultSet.getMetaData();
for (int k = 0; k < rsMetaData.getColumnCount(); k++) {
colNames[k] = rsMetaData.getColumnName(k);
}
Can anyone give me a push in the right direction? If I was unclear on anything or if you require my full code, please ask.

New Topic/Question
Reply



MultiQuote





|