jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes MySQL 4.0.17 PreparedStatement Fails! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "MySQL 4.0.17 PreparedStatement Fails!" Watch "MySQL 4.0.17 PreparedStatement Fails!" New topic

MySQL 4.0.17 PreparedStatement Fails!

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1130
I have the following PreparedStatement code:
String sql =
"insert into message " +
"(msg_pk, msg_message, msg_chtFK, msg_datetime, msg_usrFK) " +
" VALUES(null,?,?,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
System.out.println("ps = " + ps.toString());
// test values below
ps.setString(1, "Hello");
ps.setInt(2, 1 );
ps.setTimestamp(3, ts ); // SQL Timestamp
ps.setInt(4, 1);
rowsAffected = statement.executeUpdate(sql);
When I try to "executeUpdate(sql)", I'm getting the follwoing error:
"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"
If I break up this statement and don't use the PreparedStatement, the insert, with 'null' for the first parameter correctly gets MySQL to auto-increment the msg_pk field.
Also, on another table, I can just use ps.setString() and the PreparedStatemetn works then too.
I'm thinking that perhaps the insert can't handle the TimeStamp or something.
Anybody run into this behavior?
Thanks very much in advance.
-- Mike
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

PreparedStatement ps = connection.prepareStatement(sql);
rowsAffected = statement.executeUpdate(sql);

1. You are executeUpdating a statement object that doesn't exist. Also, you don't supply the executeUpdate with a parameter. So that line should probably read something like:

GenRocket - Experts at Building Test Data
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1130
I Prepared the statement earlier in the code.
However, your observation about including the sql statment in the update is something I kept missing yesterday.
That was the problem!
- Mike
I agree. Here's the link: http://aspose.com/file-tools
subject: MySQL 4.0.17 PreparedStatement Fails!
It's not a secret anymore!