amit4444
amit4444

Reputation: 5259

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

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.

Upvotes: 2

Views: 412

Answers (2)

Jeanne Boyarsky
Jeanne Boyarsky

Reputation: 12266

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.

Upvotes: 1

Daniel
Daniel

Reputation: 28104

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

Upvotes: 0

Related Questions