I\'m trying to execute a query using a PreparedStatement in Java.
I am getting error number 1064 when I try to execute my query (syntax error).
I have teste
If you look at the javadocs for Statement
(the superclass of PreparedStatement
), the method docs for executeQuery(String)
and executeUpdate(String)
say this:
Note: This method cannot be called on a
PreparedStatement
orCallableStatement
.
That's what you are doing here: calling executeQuery(String)
from Statement
on a PreparedStatement
object.
Now since the javadocs say that you "cannot" do this, actual behavior you get is unspecified ... and probably JDBC driver dependent. In this case, it appears that the MySQL driver you are using is interpreting this to mean that you are doing the update as a non-prepared statement, so that the ?
tokens are NOT interpreted as parameter placeholder. That leads the server-side SQL parser to say "syntax error".
(It would be easier for programmers if a different unchecked exception was thrown by the MySQL driver if you did this; for example UnsupportedOperationException
. However, the standard JDBC javadocs don't say what should happen in this situation. It is up to the vendor what their drivers will do.)