• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MySQL 4.0.17 PreparedStatement Fails!

 
Mike London
Ranch Hand
Posts: 1185
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Mike London
Ranch Hand
Posts: 1185
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic