• Post Reply Bookmark Topic Watch Topic
  • New Topic

update command giving error through JDBC  RSS feed

 
Ranch Hand
Posts: 35
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 4499
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 35
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 35
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 4499
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you print the prepared statement before setting the Strings?  Is users.id a String or an integer?
 
Bartender
Posts: 1616
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 1616
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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: 35
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 35
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 35
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
machines help you to do more, but experience less. Experience this tiny ad:
ScroogeXHTML 7.1 - RTF to HTML5 / XHTML converter
https://coderanch.com/t/690611/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!