Mysql 更新行时发生SQL错误
审议中的表格详情: 用于更新行的代码为:Mysql 更新行时发生SQL错误,mysql,prepared-statement,mysql-error-1064,Mysql,Prepared Statement,Mysql Error 1064,审议中的表格详情: 用于更新行的代码为: public void updateEmployeeDetails(Employee employee) { Connection conn = JDBCUtility.getConnection(); try { String updateSql = "UPDATE EMPLOYEE_INFO SET emp_name=?, location=?, email=?, dob=? WHERE emp_id=?";
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的varchar45是否与Java的字符串兼容?i、 e.ps.setStringcolumn_索引字符串是否可能是错误的根本原因
调试1:
考虑到无效的可能性,我尝试了以下几点:
// 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);
但仍然显示相同的输出。我通常会做的是:
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是什么吗?逐行验证后,我在下面的行中发现了错误:
ps.executeUpdate(updateSql);
结论:在这种情况下,在没有通过任何论证的情况下,需要进行测试
正式文件:
也许某个字符串为空,而这篇文章与此相关@DrCopyPaste在我的问题I did System.out.printlneployee.getEmp_name+employee.getDob中查看调试信息;结果不是空的。它给出了我在表格中输入的内容。所以在/@KNU中没有错误,你试过我的答案了吗?同样,按照你的建议重新排列语句也不会有帮助,因为我在运行的同一个类中有其他函数以完全相同的方式处理其他查询fine@KNU实际上有一些变化,但还行
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