如何解决在Select查询中注入单个参数时,准备好的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。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。