简体   繁体   English

JDBC查询中的错误

[英]Error in JDBC query

sql  = "select milk_rate from special_milk_rate 
        where code_producer_id=? and effective_from <= ? 
        and effective_till >= ?"

I am getting error in this query when in my JDBC code 使用JDBC代码时,此查询出现错误

Exception:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; 异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误。 check the manual that corresponds to your MySQL server version for the right syntax to use near '? 检查与您的MySQL服务器版本相对应的手册,以获取在'?附近使用的正确语法。 and effective_from <= ? 和effective_from <=? and effective_till >= ?' 和有效截止>>?' at line 1 在第1行

But when I run it in MySQL command prompt with some values it runs fine there. 但是,当我在MySQL命令提示符中使用一些值运行它时,它在那儿运行良好。

ResultSet rst= prest.executeQuery(sql);

This is incorrect. 这是不正确的。 It should be executeQuery(). 它应该是executeQuery()。

The sql gets passed to execute when using a Statement. 使用Statement时,将传递sql以执行。 It gets passed to the PreparedStatement instead when using binding variables. 使用绑定变量时,它将传递给PreparedStatement。 Since you are using the Statement API, JDBC is executing it without substituting in your binding variables and wondering what to do with the literal question marks. 由于您使用的是Statement API,因此JDBC在执行它时不会替换绑定变量,并且想知道如何使用文字问号。

您必须使用准备好的语句而不是“普通”语句,才能将问号替换为参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM