4 messages in com.mysql.lists.javaInsert/update preparedStatement bind ...
FromSent OnAttachments
Viva Chu19 Apr 2004 22:43 
Viva Chu23 Apr 2004 15:07 
Mark Matthews23 Apr 2004 15:21 
Viva Chu23 Apr 2004 15:27 
Subject:Insert/update preparedStatement bind variables
From:Viva Chu (viva@stanford.edu)
Date:04/19/2004 10:43:47 PM
List:com.mysql.lists.java

I can't get insert or update prepared statements to work if I set bind variables through my prepard statement. However, if I include the variables inline the SQL then my insert/updates work fine. I am using MySQL 4.0.18 and Jconnector 3.0.11. Any ideas on what I am doing wrong would be very much appreciated. Oddly setting bind variables to regular select statements work fine.

Code... StringBuffer sb = new StringBuffer(); sb.append("update users set first_name=? where id=1"); Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(sb.toString()); statement.setString(1, "Viva");

Throws...

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 '? where id=1' at line 1" at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1905) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1109) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1203) at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1164) at com.mysql.jdbc.Connection.execSQL(Connection.java:2087) at com.mysql.jdbc.Connection.execSQL(Connection.java:2037) at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1252) at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1198) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(Delegating PreparedStatement.java:208) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(Delegating PreparedStatement.java:208)