0

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();
                }
        }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jaskaran S.P
  • 1,055
  • 2
  • 14
  • 15

3 Answers3

1

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.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
1

1) XxxTableModel is re_created in endless loop, a new model on every of loops

2) don't to create a new XxxTableModel, nor inside try - catch - finally block

jTable1.setModel(new javax.swing.table.DefaultTableModel(

3) not reason include with

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" }
));

4) reuse XxxTableModel

5) then to add a new row inside JDBC to the XxxTableModel that is already initalized as local variable

6) there is potential issue with Concurency in Swing all updates to the already visible GUI must be done on Event Dispatch Thread

  • by wrapping inside invokeLater

  • invoke JDBC from Runnable#Thread (by wrapping inside invokeLater for each of row)

  • invoke from SwingWorker

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

You muste create only one TableModel for the whole ResultSet.

Toilal
  • 3,301
  • 1
  • 24
  • 32