PreparedStatement 語法錯誤:「
使用 Java PreparedStatements 時遇到「prepared 語法錯誤」可能會令人沮喪。如果您遇到錯誤:
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 '?)' at line 1
,程式碼如下:
String selectSql1 = "SELECT `value` FROM `sampling_numbers` WHERE `value` < (?)" ; ResultSet rs1 = con.select1(selectSql1,randNum);
,這是因為您錯誤地呼叫了Statement.executeQuery(String) 而不是PreparedStatement.executeQuery() 。解決方法如下:
this.stmt = con.prepareStatement(sql); // Prepares the Statement. stmt.setInt(1, randNum); // Binds the parameter. // return this.stmt.executeQuery(sql); // calls Statement#executeQuery return this.stmt.executeQuery(); // calls your prepared PreparedStatement
透過更改此行,您將正確執行PreparedStatement並避免「preparedStatement語法錯誤」。
以上是為什麼我的PreparedStatement在\'?\'附近拋出語法錯誤:一個Java MySQL問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!