-1

I can only display first row on all my resultset in Jtable, but I can compute all the sum of related rows in resultset. I want to display all rows in Jtable. How?

import net.proteanit.sql.DbUtils; // use for jTable
import javax.swing.table.DefaultTableModel; // use for jTable

private void showActionPerformed(java.awt.event.ActionEvent evt){                                     
PreparedStatement myStmt2;
PreparedStatement myStmt3;
PreparedStatement myStmt2a;
PreparedStatement myStmt3b;
PreparedStatement myStmt4;
PreparedStatement myStmt5;
String pizzatotalprice="0.00";
String drinkstotalprice="0.00";
String pizzatype="";
String pizzachosen="";
String topdetails="";
String crusttype="";
String pizzasize="";
String pizzaquantity="";
String drinkchosen="";
String drinksize="";
String drinkquantity="";
DefaultTableModel model = (DefaultTableModel)pizzaorderlist.getModel();
            try {
                 String query = "Select max(id) from customer ";

                 Class.forName("com.mysql.jdbc.Driver");

                 Connection con = DriverManager.getConnection("jdbc:mysql://localhost/pizzabaseaccount", "root", "");

                 Statement st = con.createStatement();

                 ResultSet rs = st.executeQuery(query);
                 rs.next();
                 String id = rs.getString("max(id)");


                 myStmt2= con.prepareStatement("Select *,sum(PizzaTotalPrice) from pizzaorder where ID = ? AND Confirmation='Unconfirmed' group by Customer");
                 myStmt2.setString(1,id);
                 ResultSet rs2 = myStmt2.executeQuery();
                   pizzaorderlist.setModel(DbUtils.resultSetToTableModel(rs2));
                 myStmt2a= con.prepareStatement("Select Customer,sum(PizzaTotalPrice) from pizzaorder where ID = ? AND Confirmation='Unconfirmed' group by Customer");
                 myStmt2a.setString(1,id);
                 ResultSet rs2a = myStmt2a.executeQuery();
                 if(rs2a.next())
                    pizzatotalprice = rs2a.getString("sum(PizzaTotalPrice)");

                 myStmt3= con.prepareStatement("Select *,sum(DrinksPrice) from drinkorder where id = ? AND Confirmation='Unconfirmed' group by Customer");
                 myStmt3.setString(1,id);
                 ResultSet rs3 = myStmt3.executeQuery();
                 drinksorderlist.setModel(DbUtils.resultSetToTableModel(rs3));

                 myStmt3b= con.prepareStatement("Select Customer,sum(DrinksPrice) from drinkorder where id = ? AND Confirmation='Unconfirmed' group by Customer");
                 myStmt3b.setString(1,id);
                 ResultSet rs3b = myStmt3b.executeQuery();
                 if(rs3b.next())
                    drinkstotalprice = rs3b.getString("sum(DrinksPrice)");
                 if(show.isSelected()){  
                 pizzaprice.setText(String.format("%.2f",Double.parseDouble(pizzatotalprice)));
                 drinksprice.setText(String.format("%.2f",Double.parseDouble(drinkstotalprice)));
                 price.setText(String.format("%.2f",Double.parseDouble(pizzatotalprice)+Double.parseDouble(drinkstotalprice)));
                 cancel.setEnabled(true);
                 confirm.setEnabled(true);
                 }

       //7th step
       con.close();
   }
   catch (Exception ex) {
        ex.printStackTrace();     
                }

// TODO add your handling code here:
}  
AstroCB
  • 12,337
  • 20
  • 57
  • 73
Nerdy Cat
  • 3
  • 1
  • 5

1 Answers1

0

You never iterate through your ResultSet properly. You should be using a while loop, and loop through the ResultSet until the next() method returns false. Within the loop, extract the data from that row of the ResultSet an use the data to create a new row for your TableModel.

Edit: I see via searching that this is a very common question on this site, and you can gain much be going through that search result: java swing jtable resultset

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373