【发布时间】: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);
但仍然显示相同的输出。
【问题讨论】:
-
也许某个字符串是
null,这篇文章与此相关吗? stackoverflow.com/a/1357449/2186023 -
@DrCopyPaste 查看我的问题中的调试信息
标签: mysql prepared-statement mysql-error-1064