2017-05-05 6 views
0

こんにちは、私は自分のタイプのエラーをSQLステートメントでキャッチしようとしています。私の方法では、在庫を持たない製品を売る人を止める何らかの方法を追加したいと注文します。私のコードはまだそれを行いません。私は注文を追加し、在庫量を更新することができますが、私は動作するifステートメントを追加する方法を考え出していません。以下はjavaでmysqlステートメントをキャッチするエラー

@FXML 
public void AddOrder(ActionEvent event) { 

String orderID = orderBox.getText(); 
String customerID = customerBox.getText(); 
String productID = productBox.getText(); 
String amount = amountBox.getText(); 
// String cost = costBox.getText(); 
String date = dateBox.getText(); 
PreparedStatement sample; 

dc = new Database(); 

try { 
    c = dc.Connect(); 

    sample = c.prepareStatement("Select stockAmount from inventory WHERE productID = ?"); 

    ResultSet rs = sample.executeQuery(); 

    while (rs.next()) { 

     Integer amount2 = rs.getInt("Amount"); 

     if (amount2 <= 0) { 

      System.out.println("No stock exists"); 

     } else { 

      query = c.prepareStatement(
        "INSERT INTO ordertable (orderID,customerID,productID,Amount,Date)VALUES(?,?,?,?,?)"); 
      update = c.prepareStatement("UPDATE inventory set stockAmount = stockAmount-? WHERE productID =?"); 
      update.setString(1, amount); 
      update.setString(2, productID); 
      update.execute(); 
      query.setString(1, orderID); 
      query.setString(2, customerID); 
      query.setString(3, productID); 
      query.setString(4, amount); 
      // query.setString(5, cost); 
      query.setString(5, date); 
      query.executeUpdate(); 
      // update.executeUpdate(); 
      Alert confirmation = new Alert(Alert.AlertType.CONFIRMATION, "Saved"); 
      // closeConfirmation.showMessageDialog(this, "Saved"); 
      confirmation.show(); 
     } 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

orderBox.clear(); 
customerBox.clear(); 
productBox.clear(); 
amountBox.clear(); 

dateBox.clear(); 
loadDataFromDatabase(event); 
} 

あなただけのこれらの行をチェックし、そのせいで例外の問題が発生した場合、私は

java.sql.SQLException: No value specified for parameter 1 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) 
    at 

答えて

0

を取得していますエラーです:

sample = c.prepareStatement("Select stockAmount from inventory WHERE productID = ?"); 

    ResultSet rs = sample.executeQuery(); 

問合せが受け入れるように定義されていますプロダクトIDのパラメータですが、文を実行する前に値を指定していません。パラメータを必要としないように、またはパラメータに値を割り当てるようにクエリを変更する必要があります。

sample = c.prepareStatement("Select stockAmount from inventory WHERE productID = ?"); 
    sample.setString(1, productID); 
    ResultSet rs = sample.executeQuery(); 
+0

よろしくお願いします。それは今コンパイルされ、コードの残りの部分を実行することができますが、在庫量が0またはそれ以下になるとアイテムを売ることができます。 –

関連する問題