• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MySQL 4.0.17 PreparedStatement Fails!

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic