【问题标题】:Error when updating MySQL database using UPDATE - SET - WHERE method in Eclipse在 Eclipse 中使用 UPDATE - SET - WHERE 方法更新 MySQL 数据库时出错
【发布时间】:2021-10-30 18:10:38
【问题描述】:

我正在使用 Eclipse 制作一个程序,该程序允许用户在每次补货/使用时更新化学品的数量,这要求他们输入化学品的 ID 以及他们想要添加/减去的数量。然后执行查询以在数据库中搜索化学品的 ID,并相应更新其数量。

但是,我在更新音量时遇到了困难。我尝试将 MySQL 的 UPDATE 语句从this website 调整为 SET volume = volume + amount added, WHERE Chemical ID = ID by the user;但是,我的代码中似乎存在一些语法错误,更具体地说是在 UPDATE - SET - WHERE 行:

public void IDEnter() {
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8889/StockControlSystem","root","root");
        Statement stmt = con.createStatement();
        String sql = "Select * from Chemicals where `Chemical ID` ='" + txtChemical_ID.getText()+"'";
        ResultSet rs = stmt.executeQuery(sql);
        if(rs.next()) {
            stmt.executeUpdate("UPDATE Chemicals" + "SET `Volume` = rs.getInt(Volume) + Integer.parseInt(AmountAdded.getText()) WHERE `Chemical ID` in (txtChemical_ID.getText())");
        }
        else {
            JOptionPane.showMessageDialog(null, "Invalid chemical ID");
            txtChemical_ID.setText(null);
        }
    } catch(Exception exc) {
        exc.printStackTrace();
    }
}

由于我还是 MySQL 的新手,有人可以帮我纠正这个问题吗?非常感谢您的帮助!

【问题讨论】:

    标签: java mysql database eclipse syntax


    【解决方案1】:

    我认为您的问题可能与“rs.getInt(Volume)”有关

    你的:

        "UPDATE Chemicals" + "SET `Volume` = rs.getInt(Volume)
    + Integer.parseInt(AmountAdded.getText()) 
    WHERE `Chemical ID` in (txtChemical_ID.getText())"
    

    你能试试这个吗:

     "UPDATE Chemicals" + "SET `Volume` = " + 
    Integer.parseInt(AmountAdded.getText()) + " 
    WHERE `Chemical ID` in (" + (txtChemical_ID.getText()) +")"
    

    【讨论】:

      【解决方案2】:

      您的整个查询格式错误。将您的代码更改为:

      stmt.executeUpdate("UPDATE Chemicals SET Volume = " +
       rs.getInt(Volume) + Integer.parseInt(AmountAdded.getText())
       + " WHERE Chemical_ID in (" + txtChemical_ID.getText() +  ")");
      

      在查询中定义列名时不能使用' 单引号。单引号用于字符串值!

      不过,这不是最好的方法。使用PreparedStatement

      这边:

      String updateString = "UPDATE Chemicals SET Volume = ? WHERE Chemical_ID in (?)"; // Creation of the prepared statement, the ? are used as placeholders for the values
      PreparedStatement preparedStatement = con.prepareStatement(updateString);
      preparedStatement.setInt(1, rs.getInt(Volume) + Integer.parseInt(AmountAdded.getText())); // Setting the first value
      preparedStatement.setString(2, txtChemical_ID.getText()); // Setting the second. I am supposing that this txtChemical_ID textField has values seperated by commas, else this will not work!
      preparedStatement.executeUpdate();
      

      如果您需要阅读更多关于PreparedStatement 的信息,那么这里有很多很棒的resources。它们还可以防止 SQL 注入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        • 2020-09-22
        • 2015-07-25
        • 2012-08-16
        相关资源
        最近更新 更多