【问题标题】:how can I make my sql update statement work?如何使我的 sql 更新语句工作?
【发布时间】:2022-06-11 05:23:17
【问题描述】:

我的代码不会更新数据库,尽管我已经尝试更改它的各个方面以找到错误请帮助我可以打印集合,但我无法更新和更改它。我试过prepareStatementcreateStatement

public void update() throws SQLException {
    try {
        PreparedStatement preparedStatement = connection.prepareStatement(
            "UPDATE main_table SET status=? WHERE ID=1"
        );
        preparedStatement.setInt(1, 1);
        preparedStatement.executeUpdate();
    }
    catch (SQLException e) {
        System.out.println("Could not update data to the database " + e.getMessage());
    }
}

【问题讨论】:

  • SQL 部分看起来不正常。您是否尝试将其设置为 NULL?即:"UPDATE main_table SET status=NULL WHERE ID=1"。如果您尝试将其设置为问号,请将问号放在单引号中,如下所示:'?'.
  • 您是否遇到任何异常?是否有 ID=1 的记录?
  • 关闭你的PreparedStatement(try-with-resources 是你的朋友)。您还需要关闭Connection(在某些时候)以确保提交更新

标签: java mysql sql


【解决方案1】:

我还会检查您的表是否有任何可能阻止您的约束,虽然那里的代码看起来可以正常工作,但我们无法看到您的数据库连接。

但这是一个工作示例,希望能突出您的问题的原因?

我有一张学生

然后我打印出表格内容

public static void main(String[] args){

    /* Print only students who are 18 and over */
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/jdbc-example","root","password");
    String queryAges = "Select * from students where age >= ?";
    PreparedStatement agesStmt = conn.prepareStatement(queryAges);
    agesStmt.setInt(1, 18);
    ResultSet rs = agesStmt.executeQuery();

    while(rs.next()){
        System.out.printf("ID: %d, Name: %s, Age: %d\n",
            rs.getInt("id"),
            rs.getString("name"),
            rs.getInt("age"));
    }
    rs.close();
    agesStmt.close();
    conn.close();

}

输出

ID: 1, Name: Henry Holt, Age: 47
ID: 2, Name: Barry McLarry, Age: 32
ID: 5, Name: Gail Emmerson, Age: 24

更新学生的年龄,然后打印该学生记录

public static void main( String[] args ) throws SQLException {

    /* Change Henry's age to 48 */
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/jdbc-example", "root", "password");
    String queryAges = "update students set age = ? where (name = ?)";
    PreparedStatement updateStmt = conn.prepareStatement(queryAges);
    updateStmt.setInt(1, 48);
    updateStmt.setString(2, "Henry Holt");
    int status = updateStmt.executeUpdate();
    if (status == 1) {
        PreparedStatement fetch = conn.prepareStatement("select * from students where name = ?");
        fetch.setString(1, "Henry Holt");
        ResultSet rs = fetch.executeQuery();
        while (rs.next())
            System.out.printf("ID: %d, Name: %s, Age: %d\n",
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getInt("age"));
        rs.close();
    }
    updateStmt.close();
    conn.close();
}

输出

ID: 1, Name: Henry Holt, Age: 48

希望对你有帮助

【讨论】:

    【解决方案2】:

    status 列是否接受表main_table 中的NULL 值?您不能在不低于它的列中输入NULL

    【讨论】:

    • 状态栏是否可以变为空(我都试过了)它仍然无法运行,我也没有遇到异常。它只是不会改变价值。
    • 尝试检查是否有任何行正在更新updateCount = preparedStatement.executeUpdate();
    猜你喜欢
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 2015-05-09
    • 2021-12-01
    相关资源
    最近更新 更多