2016-06-24 11 views
-2

Ich versuche, die Spalte quantityTaken für jedes itemNo, und versuchen, es einmal für jedes itemNo in der Tabellenansicht anzuzeigen, wird jede Hilfe geschätzt werden. Dies ist mein Code: ich hoffe, dass Sie meine Bitte verstehenVersucht, Spalte aus einer Tabelle zusammenzufassen MySql

@FXML 
    private void populateTableView() { 
     int totalQuantityTaken = 0; 

     try { 

      /* SQL QUERY */ 
      String sql = "SELECT employeeName, itemNo, itemName, timeTaken, quantityTaken FROM UsedProduct\n" + 
        "INNER JOIN Employee ON\n" + 
        "UsedProduct.employeeBarcode = Employee.employeeBarcode\n" + 
        "INNER JOIN Item ON\n" + 
        "UsedProduct.itemBarcode = Item.itemBarcode\n" + 
        "WHERE Item.itemNo = ?;"; 

      /* EXECUTION OF QUERY */ 
      String inputBarcode = tfSearch.getText(); 
      PreparedStatement preparedStatement = connectDB.preparedStatement(sql); 
      preparedStatement.setString(1, inputBarcode); 

      ResultSet result = preparedStatement.executeQuery(); 

      while (result.next()){ 
       int quantityTaken = result.getInt("quantityTaken"); 
       totalQuantityTaken = quantityTaken + totalQuantityTaken; 
      } 

      while (result.next()) { 

       String employeeName = result.getString("employeeName"); 
       String itemNumber = result.getString("itemNo"); 
       String itemName = result.getString("itemName"); 
       String timeTaken = result.getString("timeTaken"); 
       //String quantityTaken = result.getString("quantityTaken"); 
       UsedProductObj usedProductObj = new UsedProductObj(employeeName, itemNumber, itemName, timeTaken, totalQuantityTaken); 

       searchData.addAll(usedProductObj); 

      } 
     } catch (Exception e) { 
      MainViewController.updateWarningMessage("Error"); 
      e.printStackTrace(); 
      System.out.println("Exception in searchByItemNumberInUsed() from AdminController class: " + e.getMessage()); 
     } 

     /* SETTING VALUES FROM OBJECT INTO COLUMNS */ 
     usedProductEmployeeNameColumn.setCellValueFactory(new PropertyValueFactory<SearchObj, String>("employeeName")); 
     usedProductItemNumberColumn.setCellValueFactory(new PropertyValueFactory<SearchObj, String>("itemNumber")); 
     usedProductItemNameColumn.setCellValueFactory(new PropertyValueFactory<SearchObj, String>("itemName")); 
     usedProductTimeTakenColumn.setCellValueFactory(new PropertyValueFactory<SearchObj, String>("timeTaken")); 
     usedProductQuantityColumn.setCellValueFactory(new PropertyValueFactory<SearchObj, String>("quantityTaken")); 

     /* ADDING THE OBSERVABLE LIST TO THE TABLE VIEW */ 
     usedProductTableView.getItems().setAll(searchData); 
     //searchData.clear(); //i did this because it would duplicate the last element if the item was returned 

    } 

Antwort

0

Ich habe den ganzen Code nicht lesen, aber ich denke, was Sie suchen, die GROUP BY-Funktion in MySQL ist. Etwas wie dieses:

SELECT sum(quantityTaken), itemNo FROM UsedProduct GROUP BY itemNo 
Verwandte Themen