我在 DAO 类中有以下代码:
public boolean employeeExists(String employeeCode) throws InstantiationException, SQLException {
String SQL_TEXT = "select count(*) from employee where emp_code=?;";
int scalarValue = 0;
Connection conn = DatabaseAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(SQL_TEXT);
pstmt.setString(1, employeeCode);
try {
ResultSet rs = pstmt.executeQuery(SQL_TEXT);
if (rs.next()) {
scalarValue = rs.getInt(1);
}
} catch (SQLException e) {
// TODO: log error.
throw e;
} finally {
if (pstmt != null && !pstmt.isClosed())
pstmt.close();
if (conn != null && !conn.isClosed())
conn.close();
}
return scalarValue > 0;
}
当我运行代码,即调用上述 DAO 方法时,出现以下错误:
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 '?'
语法似乎没问题,并且可以在 mysql 工作台中执行,但使用 JDBC 会引发异常。 这是 mysql jdbc 驱动程序中的某种错误吗?请帮忙。
最佳答案
您的问题在于:- ResultSet rs = pstmt.executeQuery(SQL_TEXT);
将其更改为:- ResultSet rs = pstmt.executeQuery();
关于java - 带有PreparedStatement的com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33344930/