aspose file tools
The moose likes JDBC and Relational Databases and the fly likes Can't execute prepared statements using mySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Can Watch "Can New topic
Author

Can't execute prepared statements using mySQL

JoseAntonio Quiles
Greenhorn

Joined: Jun 07, 2002
Posts: 5
I am using mySQL 4.01 and mm JDBC driver 3.05
String query = "select * from test_table where b = ?";
PreparedStatement Stmt = Conn.prepareStatement(query);
Stmt.setString(1,s);
Stmt.executeQuery(query);
I get the following exception:
java.sql.SQLException: Syntax error or access violation, message from server: "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"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1613)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:880)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:939)
at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:911)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1777)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1711)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1203)
If I don't use prepared statement, it works.
Any ideas?
thank you in advance.
Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
Try
Stmt.executeQuery();
[ February 11, 2003: Message edited by: Jon Strayer ]

Jon
JoseAntonio Quiles
Greenhorn

Joined: Jun 07, 2002
Posts: 5
That was the problem.
Thank you very much.
 
 
subject: Can't execute prepared statements using mySQL
 
jQuery in Action, 3rd edition