Search code examples
javaswingjdbcjtabledefaulttablemodel

Java JTable not showing all rows from MySQL


I have a problem of JTable, that is when more then 1 rows are returned from MySQL table the JTable shows up only 1 record - (the last one), but when I try simple System.out.print it shows all records. Heres my code:

 System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://xxx.xx.xxx.xx:3306/";
    String dbName = "smartcart";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "xxx"; 
    String password = "xxx";
    Statement stmt = null;
    ResultSet rs = null;
        cartID=jTextField1.getText().trim();
        if(cartID.matches("(\\w*)") && !cartID.isEmpty())
        {
           jLabel2.setText("Searching.....");
               try {
                        Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      stmt = conn.createStatement();
                       String query = "SELECT * FROM user_stock WHERE cartID="+cartID;
                       rs = stmt.executeQuery(query);
                       jInternalFrame1.setVisible(true);
                        while (rs.next()) {
                              jTable1.setModel(new javax.swing.table.DefaultTableModel(
                               new Object [][] {
                                  {rs.getString("id"), rs.getString("product_name"), rs.getString("product_code"), rs.getString("product_quantity"), rs.getString("product_price"), "0"}
                               },
                                new String [] {
                                  "Product No.", "Product Name", "Product Code", "Product Quantity", "Product Price", "Discount"
                                   }
                                ));
                        }
      conn.close();
      System.out.println("Disconnected from database");

                        }
               catch (Exception e) {
                  e.printStackTrace();
                }
        }

Solution

  • You are recreating a new TableModel for each returned row instead of one model for all rows:

    String query = "SELECT * FROM user_stock WHERE cartID="+cartID;
    rs = stmt.executeQuery(query);
    jInternalFrame1.setVisible(true);
    javax.swing.table.DefaultTableModel model = new javax.swing.table.DefaultTableModel();
    model.setColumnIdentifiers(new String [] {
                                  "Product No.", "Product Name", "Product Code", "Product Quantity", "Product Price", "Discount" });
     jTable1.setModel(model);
     while (rs.next()) {
         model.addRow(new Object [] {
                   rs.getString("id"), rs.getString("product_name"), rs.getString("product_code"), rs.getString("product_quantity"), rs.getString("product_price"), "0"});       
     }
    

    Caveats: I have not tested nor executed the code above but it should not be hard to fix typos or missing parenthesis.

    Now, you can also simplify your life and reuse directly ResulSetTableModel which does it all for you.