PreparedStatement syntax error
Posted by: Chris Rickey
Date: April 01, 2005 07:22PM

This is interesting. I like using prepared statements, but this is problem is really frustrating me.

If I attempt to use a prepared statement, I get the following error. However, if I change the code to include the value in the string instead of using substitution, it works. See the code below.

Problem Code:
sql = "select * from some_table where ";
sql += "obj_key = ?";
conn = this.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, 200);
rs = stmt.executeQuery(sql);

Working Code:
sql = "select * from some_table where ";
sql += "obj_key = " + 200;
conn = this.getConnection();
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery(sql);

Error with Problem Code:
java.sql.SQLException: 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 '?' at line 1

Any ideas? Thanks - Chris

Options: ReplyQuote


Subject
Written By
Posted
PreparedStatement syntax error
April 01, 2005 07:22PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.