• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query from Java class not updating MySQL database.

 
khaw hasnaj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written this code, and as far as i can see it should work, ive googled it and also tried to delete in the mysql workbench to see how to write it, and it still wont work. not cool, .

 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is the problem that you are facing? Is an exception thrown, the database is not updated as you want, or something else? Please provide us with more details in order to properly help you.
 
Campbell Ritchie
Sheriff
Posts: 51368
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added some new lines to make your code legible. I shall move this discussion to our databases forum.
 
Campbell Ritchie
Sheriff
Posts: 51368
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khaw hasnaj wrote: . . .
You are making unjustified assumptions about the reason for the Exception there. It might not be an Ingen forbindelse. (=no connection). You need to print the message and stack trace for the Exception at least, in order to know what happened.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khaw hasnaj wrote:


Are you sure the syntax of this command is valid in MySQL? The SQL standard syntax is

Moreover, you're using a PreparedStatement (which is good) and still stuffing in literal value in the where clause (which is bad and generally makes you vulnerable to SQL injection attacks, even though you're using PreparedStatement).
 
khaw hasnaj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is the problem that you are facing? Is an exception thrown, the database is not updated as you want, or something else? Please provide us with more details in order to properly help you.

The database wont update as i want it to, as you can see i have written system.out.printlns, and it doesnt go further then the sysout "after query.setString", so i assume line number 34 is at fault.


I have added some new lines to make your code legible. I shall move this discussion to our databases forum.

Okay, sorry for any inconvenience.


You are making unjustified assumptions about the reason for the Exception there. It might not be an Ingen forbindelse. (=no connection). You need to print the message and stack trace for the Exception at least, in order to know what happened.

So it should be more like a SQLException instead?, just that i have no idea when and what i use them for, i randomly write them when Eclipse says i need them.


Are you sure the syntax of this command is valid in MySQL? The SQL standard syntax is.......Moreover, you're using a PreparedStatement (which is good) and still stuffing in literal value in the where clause (which is bad and generally makes you vulnerable to SQL injection attacks, even though you're using PreparedStatement).

I might be wrong, but then ive also tried writting the code the other way you describe, still wont work :/. And I hope I'm not stuffing a literal value, as i just want to say UPDATE * WHERE the id is the number someone enters, so i dont end up updating every one of them.

And thanks for the answers, .
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make sure the query is syntactically correct, try to run it first outside Java, eg. in TOAD or mysql command line tool. Only after getting it right there, try to run the same query from Java.

You can read more about SQL injection here on the Ranch or perhaps on the Wikipedia.
 
khaw hasnaj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make sure the query is syntactically correct, try to run it first outside Java, eg. in TOAD or mysql command line tool. Only after getting it right there, try to run the same query from Java.


Thats exactly what I have done in the MySQL Workbench, where it works flawlessly, no idea what goes wrong going from Workbench to my Java code.
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khaw hasnaj wrote:
To make sure the query is syntactically correct, try to run it first outside Java, eg. in TOAD or mysql command line tool. Only after getting it right there, try to run the same query from Java.


Thats exactly what I have done in the MySQL Workbench, where it works flawlessly, no idea what goes wrong going from Workbench to my Java code.


if you just put them where eclipse wants them, leave the default code in eclipse gives you


I have also had problems with one of the my sql workbenches caching the results. i spent over 1 hour one night trying to understand why my update wasn't working.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic