开发者

Error in JDBC query

开发者 https://www.devze.com 2023-02-06 19:05 出处:网络
sql= \"select milk_rate from special_milk_rate where code_producer_id=? and effective_from <= ? and effective_till >= ?\"
sql  = "select milk_rate from special_milk_rate 
        where code_producer_id=? and effective_from <= ? 
        and effective_till >= ?"

I am gett开发者_运维知识库ing error in this query when in my JDBC code

Exception: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 '? and effective_from <= ? and effective_till >= ?' at line 1

But when I run it in MySQL command prompt with some values it runs fine there.


ResultSet rst= prest.executeQuery(sql);

This is incorrect. It should be executeQuery().

The sql gets passed to execute when using a Statement. It gets passed to the PreparedStatement instead when using binding variables. 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.


You have to use prepared statements instead of "normal" statements to have the question marks replaced by parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消