Search code examples
javaswingjtable

JTable records not displayed


I am trying to display the records on JTable when the frame is loaded, but the records didn't display. This is what I've tried so far:

public void load() {
  try {
    Connection con1;
    PreparedStatement insert;
    Class.forName("com.mysql.jdbc.Driver");
    con1 = DriverManager.getConnection("jdbc:mysql://localhost/javapos","root","");
    insert = con1.prepareStatement("SELECT name,status FROM category");
    ResultSet Rs = insert.executeQuery();
    while(Rs.next()) {
      DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
      String name = Rs.getString("name");
      String status = Rs.getString("status");                 
      model.addRow(new Object[]{name,status });                               
    }
    jTable1.setModel(model);                       
  } catch (Exception e) {
    System.out.println("Failed " + e);
  }             
}

Solution

  • I think that you are not getting the model of the table in the correct way. Try:

    DefaultTableModel model = (DefaultTableModel) tblMenu.getModel();
    

    Check this link to get more information about it.