Reputation: 11
I'm trying to display the data from a access database; for some reason, whenever I run the program, only one row is displayed. Here's my code
try{
String coursec = jTextField9.getText().trim();
String sql5 = "SELECT * FROM Studentcourse WHERE ccode ='" +coursec+"'";
String url5 ="jdbc:ucanaccess://C:/Users/james_000/Documents/NetBeansProjects/Registration/Campus.accdb ";
Connection conn5 =DriverManager.getConnection(url5);
Statement statem = conn5.createStatement();
ResultSet rs5 = statem.executeQuery(sql5);
while (rs5.next()){
DefaultTableModel dm;
dm = new DefaultTableModel(10, 10);
String coursecode= rs5.getString(2);
String attend =rs5.getString(3);
String date =rs5.getString(4);
Vector <String> vector = new Vector<String>();
vector.add(coursecode);
vector.add(attend);
vector.add(date);
String s[] = new String[]{"ccode", "stnumattend", "Date"};
dm.setColumnIdentifiers(s);
jTable1.setModel(dm);
dm.addRow(vector);
jTable1.setVisible(true);
}
}
catch(Exception d){
System.err.println("Exception:" + d.getMessage());
}
Upvotes: 1
Views: 39
Reputation: 205865
You're creating a new DefaultTableModel
each time through your while
loop, so only the last such model is applied to jTable1
. At a minimum, move the model creation out of the loop.
DefaultTableModel dm = new DefaultTableModel(10, 10);
jTable1.setModel(dm);
while (rs5.next()) {
String coursecode= rs5.getString(2);
…
}
Upvotes: 1