I am doing a Practical Assessment Task for my Grade 12 IT class. I am struggling with the code to populate my jTable (From the swing controls) in my GUI. I am using Netbeans as an IDE. The database connects using a jdbc:odbc bridge. This code is in a java class in my project.
**please note that i am relatively inexperienced when it comes to coding
Here is the code i have currently (but it gives no output in my jtable) if you can fix it, please explain what you did or if you can please give me code that will work :)
db.setConnection();
Statement stmt = null;
try {
con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}"
+ ";DBQ=src/TheChangeProjectDB.accdb");
stmt = con.createStatement();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Error: " + ex);
}
String sql = "SELECT * FROM KanyisaLearners";
try {
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(i));
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 0; i <= columns; i++) {
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
stmt.close();
} catch (Exception e) {
System.out.println(e);
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
JPanel buttonPanel = new JPanel();
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
KanyisaHoofskerm frame = new KanyisaHoofskerm();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setTitle("Learners");
frame.setVisible(true);