Jtable中的Java显示ResultSet

时间:2015-03-18 00:30:55

标签: java sql jtable resultset

我只能在Jtable中的所有结果集上显示第一行,但我可以计算结果集中所有相关行的总和。我想在Jtable中显示所有行。怎么样?

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:
}  

1 个答案:

答案 0 :(得分:0)

您永远不会正确遍历ResultSet。您应该使用while循环,并循环遍历ResultSet,直到next()方法返回false。在循环中,从ResultSet的该行中提取数据,并使用该数据为TableModel创建新行。

编辑:我通过搜索看到这是一个非常常见的问题,在这个网站上,你可以获得很多搜索结果:java swing jtable resultset