javatablemodel

Select SQL query returning only one value of array in the tableModel


I'm trying to fetch rows with commodities under a similar category name from a particular categories table in a database and, with each of the rows, I have made a second SQL query to do summation of quantities of orders made in the past one week from the orders table. I want the results to be added to a table model, but only one row is added.

private void analyticsTableQuery(String Category) {
    this.categorySelected = Category;
    categoryTitle.setText(Category);
   int ordered;
    int initial;
    String query2 = "SELECT * FROM stock INNER JOIN category ON stock.Category_id=category.id where category.Category_Name='"+Category+"'";
    try {
            con = DriverManager.getConnection("jdbc:mysql://194.5.156.94:3306/u843360242_tukule?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","u843360242_tukule","cocaine");
            pst = con.prepareStatement(query2);
            rs=pst.executeQuery();
            DefaultTableModel tm=(DefaultTableModel)analyticsTable.getModel();
            tm.setRowCount(0);
         while(rs.next()){
                int id = rs.getInt("stock.id");
                int current = rs.getInt("stock.Quantity");
                String name = rs.getString("stock.Name");
                String price = rs.getString("stock.Price");
                String query = "SELECT sum(Quantity) as 'sum' FROM `orders` WHERE Created_at >= DATE(CURRENT_DATE() -7) 
                and Stock_id ="+id; 
                try {
               con = DriverManager.getConnection("jdbc:mysql://localhost:3306/u843360242_tukule 
         useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","");
               pst = con.prepareStatement(query);
               rs=pst.executeQuery();
               rs.next();
                   if(rs.getInt("sum") > 0)
                {
                  ordered = rs.getInt("sum");
                  initial = current + ordered;
                }
                else{ 
                  ordered = 0;
                  initial = current + ordered;
                }
                   Object o[]={
                            name,
                            price,
                            initial};
                   tm.addRow(o); 
               } catch (SQLException ex) {
                   Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
               }     
           }             
     } catch (SQLException ex) {
        Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
     }  
}   

Solution

  • You are using resultset rs for both outer and inner results. re-initializing rs with inner resultset affects your outer loop condition. And even if there are still rows in rs due to which loop progresses, reading columns like id, quantity, name etc will result in exception as there columns might not be present in the new resultset rs.