• 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

update command giving error through JDBC

 
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings everyone,
I was writing this application on java with JDBC connectivity, the update command that i implemented gives an error like this

My code for update command is as follows

Here, getCon() and closeCon() are methods defined by me to open and close connection with the JDBC driver manager, users is the name of table that I am inserting the data in
I have checked the syntax in official mysql reference and also checked the syntax generated by the XAMPP control panel for updating the contents of the table
Is there something that I am missing here??
Thanks.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to send the prepared statement to the console or a log file to see exactly how it ends up, but my guess is it has to do with the "like" clause.  You probably want "=".

 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was an '=' before like, but still gave the same error that's why I tried to change it to 'like' in the first place
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I sent the preparedStatement to the console like you suggested and this is what I got
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you print the prepared statement before setting the Strings?  Is users.id a String or an integer?
 
Marshal
Posts: 4499
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is in this line: st.executeUpdate(sql);

If you include a String parameter, then you are calling the Statement#executeUpdate(String) in PreparedStatement's super class, not PreparedStatement#executeUpdate() as intended.

You want to call PreparedStatement#executeUpdate(), so remove the String parameter.

 
Ron McLeod
Marshal
Posts: 4499
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, assuming that your id field should be an exact match, you will probably want to put your where clause back to WHERE users.id = ?
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Did you print the prepared statement before setting the Strings?  Is users.id a String or an integer?


Yes, I printed the statement before setting the Strings. like this

and user.id is an integer
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:The problem is in this line: st.executeUpdate(sql);

If you include a String parameter, then you are calling the Statement#executeUpdate(String) in PreparedStatement's super class, not PreparedStatement#executeUpdate() as intended.

You want to call PreparedStatement#executeUpdate(), so remove the String parameter.


I tried it and it worked.
Thank you so much... I put so much though into it but never thought like.
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to both of you who replied and helped me and that too so quick.
This place is really good
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic