3 messages in com.mysql.lists.javaRe: Blocking PreparedStatement Exception
FromSent OnAttachments
Michel Meyer27 May 2004 13:25 
Mark Matthews27 May 2004 13:38 
Michel Meyer27 May 2004 14:51 
Subject:Re: Blocking PreparedStatement Exception
From:Mark Matthews (ma@mysql.com)
Date:05/27/2004 01:38:59 PM
List:com.mysql.lists.java

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1

Michel Meyer wrote:

Hi,

I have just switched (trying to) 4.1, --old-password and I encounter a very strange bug with PreparedStatement:

I put up a test case so that Mark can see exactly what's the pb. I think some king of similar pb have already been submitted to the list but remained unanswered.

1. The Table: MYTABLE

--------------------------------------- my_key INT primary my_col INT

2. The class

------------------ package net.pulsevision.test;

import java.sql.*;

public class testPreparedStatement { public static void main(String[] args) throws ClassNotFoundException, SQLException {

Class.forName("com.mysql.jdbc.Driver"); String connectionOptions =

"?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useServerPrepStmts=false";

Connection connection = null; String connectString = "jdbc:mysql://localhost:3306/MYDATABASE" + connectionOptions; System.out.println("Connecting to " + connectString);

connection = DriverManager.getConnection(connectString, "user", "pass");

System.out.println("Driver Version: " + connection.getMetaData().getDriverVersion());

// Do the Test String sql = "INSERT INTO MYTABLE (my_col) values (?)"; PreparedStatement stmt = connection.prepareStatement(sql); stmt.setInt(1, 666); System.out.println("stmt -> " + stmt); stmt.executeUpdate(sql);

ResultSet rs = stmt.getGeneratedKeys(); if (rs.next()) { System.out.println("generated key -> " + rs.getInt(1)); } } }

I tryed that with various versions of the connector J 3.0.12 and 3.1.1-alpha I get always the same bizarre error:

java.sql.SQLException: Syntax error or access violation message from server: "You have an error in your SQL syntax near '?)' at line 1" at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2234) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1412) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1494) at com.mysql.jdbc.Connection.execSQL(Connection.java:1905) at com.mysql.jdbc.Connection.execSQL(Connection.java:1847) at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1280) at

net.pulsevision.kioo.test.testPreparedStatement.main(testPreparedStatement.java:27)

Is this related to the fact that I use --old-password on the database ?

What can I do ? Right now, my project is stuck: I can't switch to new passwords because of code legacy.

Mark, I need your help .

Michel,

This is a common object-oriented programming error that is hard to catch...Notice how you're using executeUpdate(String), not executeUpdate()....That ends up using the code in Statement.executeUpdate() (because PreparedStatement extends Statement).

Because of this, you're sending the SQL to prepare as a plain-old query, and of course the server gets a bit confused as to what you are trying to do ;)

Change your executeUpdate(sql) to a plain old execute() and you should be good to go.

-Mark

- -- Mr. Mark Matthews MySQL AB, Software Development Manager, J2EE and Windows Platforms Office: +1 708 332 0507 www.mysql.com

MySQL Guide to Lower TCO http://www.mysql.com/it-resources/white-papers/tco.php -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.3 (MingW32) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAtlH4tvXNTca6JD8RAtNNAJ9qhJ1GmNBiMkJQR7XjvpjskDvj2wCcCu0K W8EHxnjDlhg10CU9UjGLFCs= =vicJ -----END PGP SIGNATURE-----