File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC 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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "MySQL 4.0.17 PreparedStatement Fails!" Watch "MySQL 4.0.17 PreparedStatement Fails!" New topic
Author

MySQL 4.0.17 PreparedStatement Fails!

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
I have the following PreparedStatement code:
String sql =
"insert into message " +
"(msg_pk, msg_message, msg_chtFK, msg_datetime, msg_usrFK) " +
" VALUES(null,?,?,?,?)";
try
{
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?
Suggestions?
Thanks very much in advance.
-- Mike
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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 - A Test Data Generation Platform
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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!
THANK YOU!!!
- Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MySQL 4.0.17 PreparedStatement Fails!
 
Similar Threads
Timestamp assistance
Java Code does not recognize my sequence name
Prepared Statement problem in my servlet's data manager
Prepared Statement
Inserting date into an MS Access DB