PreparedStatement throws MySQLSyntaxErrorException

Posted by sqlBugs on Stack Overflow See other posts from Stack Overflow or by sqlBugs
Published on 2010-05-11T15:24:39Z Indexed on 2010/05/16 6:00 UTC
Read the original article Hit count: 170

Filed under:
|
|

I have the following Java code:

String query = "Select 1 from myTable where name = ? and age = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, name);
stmt.setInt(2, age);
ResultSet rs = stmt.executeQuery();

Whenever I run the above code, it throws an exception on line 2 (where the PreparedStatement is declared):

Error 0221202: SQLException = com.mysql.jdbc.exceptions.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 age = ?' at line 1

Does anyone have any suggestions about what I am doing wrong? Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about mysql