2
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.

templatetypedef
376k113 gold badges954 silver badges1.1k bronze badges
asked Jan 23, 2011 at 7:15
3
  • 1
    You should add tags like mysql, jdbc, and sql to get more answers. Commented Jan 23, 2011 at 7:18
  • 1
    query's fine, there is a problem with your PreparedStatement. Show the Java code. Commented Jan 23, 2011 at 7:20
  • Hey can anyone tell me what do this query?? Commented Jan 23, 2011 at 12:19

2 Answers 2

1
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.

answered Jan 23, 2011 at 18:18
Sign up to request clarification or add additional context in comments.

Comments

0

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

answered Jan 23, 2011 at 7:22

3 Comments

hi i am using preparestatement my code is sql = " select milk_rate from special_milk_rate where code_producer_id='? and effective_from <= '? and effective_till >= '?" prest=conn.prepareStatement(sql) prest.setLong(1,id) prest.setDate(2,sqlDate) prest.setDate(3,sqlDate) ResultSet rst= prest.executeQuery(sql);
Get rid of the single quotes in your prepared statement.
@Costis is correct. In addition, you appear to be missing the semicolons after each statement.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.