在Select Query中注入单个参数时,Prepared Stetement参数出现问题



我正在尝试为选择查询创建准备好的语句

String Select_Query = "select * from customers where customerNumber=? ";
Connection connection = DriverManagersSQL.getDriverMangerInstance();
preparedStatement statement = connection.prepareStatement(Select_Query);
statement.setInt(1, 101);
ResultSet resultSet = statement.executeQuery(Select_Query);

但我有一个这样的错误:

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

我在数据库中运行了查询,它运行良好。没有语法错误。当我尝试与单个过滤参数一起使用时,这种情况总是发生。如有任何建议,我们将不胜感激。

感谢

不要调用

statement.executeQuery(Select_Query)

您已经设置了PreparedStatement。它已经得到了你的SQL和里面的参数

只需致电

statement.executeQuery()

相反。

executeQuery((是PreparedStatement的一个方法,它将使用您已经设置的参数执行您已经给它的查询。

executeQuery(String(是Statement的一种方法,它试图执行您现在给它的查询。它不使用您的参数,文档特别指出,如果您在PreparedStatement上调用此方法,它将导致SQLException。

最新更新