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