mysql - 在java中捕获mysql语句时出错

标签 mysql

你好,所以我正在尝试编写我自己的 sql 语句捕获错误类型。在我的方法添加订单中,我想添加某种方法来阻止某人销售没有库存的产品。我下面的代码还没有做到这一点。我可以添加订单并更新库存数量,但我还没有找到添加有效的 if 语句的方法。

@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 

最佳答案

如果您只是因为该异常而遇到麻烦,请检查以下几行:

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

    ResultSet rs = sample.executeQuery();

该查询被定义为接受 ProductID 参数,但您在执行语句之前尚未为其指定值。您需要更改查询,以便它不需要参数或为参数分配值,如下所示:

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

关于mysql - 在java中捕获mysql语句时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795430/

相关文章:

php - 将数据从一列移动到另一列 mysql

mysql - 如何将Access MDB格式数据库导入MySQL?

php - 将内容加载到应用程序的最佳方式

php - 从 MySQL 获取最新记录(7 天)

php - 使用 php 获取显示 mysql 查询

mysql - 是否可以在 MySQL 表上执行内联 GROUP BY?

Python导入MySQLdb : Library not loaded: libmysqlclient. 18.dylib

php - 在 Laravel 中将 Eloquent 集合与查询合并?

mysql - 关于从某人关注的用户获取最新帖子的可扩展性问题 : two different implementations

结合子查询和 JOIN 的 MySQL 语句不起作用