Re: PreparedStatement syntax error
Posted by: Mark Matthews
Date: April 01, 2005 08:27PM

Chris Rickey wrote:
> 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);
^^^^^^^^^^^^^^^^^^^^^^^^^^

Chris,

Should be rs = stmt.executeQuery(); // i.e. no parameters

You're basically passing the query with the '?' in it to the _super_ interface implementation of executeQuery(String) in java.sql.Statement, which of course doesn't know what to do with placeholders.

I agree it's confusing, but it's also how inheritence in OO works ;)

-Mark

Mark Matthews
Consulting Member Technical Staff - MySQL Enterprise Tools
Oracle
http://www.mysql.com/products/enterprise/monitor.html

Options: ReplyQuote


Subject
Written By
Posted
Re: PreparedStatement syntax error
April 01, 2005 08:27PM


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.