• Post Reply Bookmark Topic Watch Topic
  • New Topic

update command giving error through JDBC  RSS feed

 
Mishra Saurabh
Greenhorn
Posts: 22
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.
 
Knute Snortum
Sheriff
Posts: 4081
112
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
Greenhorn
Posts: 22
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
Greenhorn
Posts: 22
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: 4081
112
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?
 
Ron McLeod
Saloon Keeper
Posts: 1600
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
Saloon Keeper
Posts: 1600
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
Greenhorn
Posts: 22
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
Greenhorn
Posts: 22
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
Greenhorn
Posts: 22
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!