我花了好幾個小時看着什麼應該是完美的代碼。該方法的connection.createStatement()
版本可以正常工作,但只要我嘗試將其轉換爲更好的connection.prepareStatement()
版本,就會引發MySQLSyntaxErrorException並抱怨'?'附近有問題。字符在我的查詢字符串中。該代碼張貼在下面,我根本無法看到它的問題。數據庫字段是VARCHAR並接受字符串,所以這不是問題。PreparedStatement MySQLSyntaxErrorException使用有效的查詢字符串
public Discussion getDbDiscussionInstance(String _instanceId) throws SQLException {
String qryStr = null;
PreparedStatement myStmt = null;
ResultSet myRs = null;
// Try to build the object with existing data.
try {
qryStr = "SELECT assignment_id, discussion_id, section_id, user_id, circle_id, breakout_id, title, description, created, due FROM macb_discussions WHERE instance_id=?";
myStmt = connection.prepareStatement(qryStr);
myStmt.setString(1, _instanceId);
myRs = myStmt.executeQuery(qryStr);
if (rs.next()) {
this.discussionId = myRs.getString("discussion_id");
}
} catch (SQLException e) {
dbFunc.catchSQLException(e);
} finally {
myRs.close();
myStmt.close();
}
}
可以使用包括確切的異常 – 2014-11-21 13:57:13