PreparedStatement 语法错误:“
在使用 Java PreparedStatements 时遇到“preparedStatement 语法错误”可能会令人沮丧。如果您遇到错误:
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中文网其他相关文章!