【问题标题】:SQL error while updating a row更新行时出现 SQL 错误
【发布时间】:2014-06-09 10:40:41
【问题描述】:

正在考虑的表格的详细信息:

用于更新行的代码是:

 public void updateEmployeeDetails(Employee employee) {

    Connection conn = JDBCUtility.getConnection();

    try {
        String updateSql = "UPDATE EMPLOYEE_INFO SET emp_name=?, location=?, email=?, dob=? WHERE emp_id=?";

        PreparedStatement ps = (PreparedStatement)conn.prepareStatement(updateSql);
        ps.setString(1, employee.getEmp_name()); /*all the attributes of employee object are of type String */
        ps.setString(2, employee.getLocation());
        ps.setString(3, employee.getEmail());
        ps.setString(4, employee.getDob());
        ps.setString(5, employee.getEmp_id());
        ps.executeUpdate(updateSql);

    } catch (SQLException e) {
        System.out.println(e.getErrorCode());
        e.printStackTrace();
    }
}

收到错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, location=?, email=?, dob=? WHERE  emp_id=?' at line 1

谁能指出错误,因为我仔细检查了查询字符串中的任何可能错误? mysql的varchar(45)和Java的String兼容吗?即ps.setString(column_index, String) 会是错误的根本原因吗?

调试 1:

考虑到null 的可能性,我尝试(徒劳)如下:

//  ps.setString(1, employee.getEmp_name());
//  ps.setString(2, employee.getLocation());
//  ps.setString(3, employee.getEmail());
//  ps.setString(4, employee.getDob());
//  ps.setString(5, employee.getEmp_id());

ps.setString(1, "Superman");
ps.setString(2, "Sky");
ps.setString(3, "anymail@dooodle.com");
ps.setString(4, "1-1-0111");
ps.setString(5, "501");
ps.executeUpdate(updateSql);

但仍然显示相同的输出。

【问题讨论】:

标签: mysql prepared-statement mysql-error-1064


【解决方案1】:

逐行验证后,我发现以下行有错误:

ps.executeUpdate(updateSql);


结论:在这种情况下,executeUpdate() 是必需的,没有传递任何参数

官方文档:

int executeUpdate() throws SQLException

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Returns:
either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

【讨论】:

    【解决方案2】:

    我通常会做的是:

    private static final String UPDATE_ITEM =
            "UPDATE `EMPLOYEE_INFO` SET emp_name=?, location=?, email=?, dob=? WHERE emp_id=?";
    
    public void updateEmployeeDetails(Employee employee) {
    
        Connection conn = null;
        PreparedStatement ps = null;
    
        try {
    
            conn = JDBCUtility.getConnection();
            ps = conn.prepareStatement(UPDATE_ITEM);
            ps.setString(1, employee.getEmp_name()); /*all the attributes of employee object are of type String */
            ps.setString(2, employee.getLocation());
            ps.setString(3, employee.getEmail());
            ps.setString(4, employee.getDob());
            ps.setString(5, employee.getEmp_id());
            ps.executeUpdate();
    
        } catch (SQLException e) {
            System.out.println(e.getErrorCode());
            e.printStackTrace();
        }
    }
    

    但是你能仔细检查一下 employee.getEmp_name() 的返回值吗?

    【讨论】:

    • 我做了System.out.println(employee.getEmp_name()+employee.getDob());,结果不是null。它给出了我在表格中输入的内容。所以没有错误/
    • @KNU 你试过我的回答了吗?
    • 您建议的重新洗牌语句也不应该有帮助,因为我有其他功能以完全相同的方式(在同一类中)运行良好的其他查询
    • @KNU 实际上有一些变化,但没关系。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 2018-12-13
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多