File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

updating fields with prepared statement and looping

 
Jhedzkie Skies
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there.

i am doing a simple database and i sort of got my code lack something...

i've written this method to update values on my table






these piece of codes are working except for one thing..
it seems that it doesnt execute the last update on the loop. (the 5th update.)
thus, only updating the DB table 4 times.



thanks in advance
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jhedzkie,
I think that is happening because you aren't explicitly committing or closing the statement or connection. The driver doesn't know you are done. It is good practice to close resources in a finally block regardless so I would recommend adding that.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rest of this isn't the problem; just a note on good practices. It is better to use a prepared statement. Which you don't really do. A prepared statement should have "?" objects for the parameters rather than concatenating them. Then use stmt.setString() or stmt.setInt() to set them. Also, this approach lets you use executeBatch to update multiple rows more efficiently.
 
Jhedzkie Skies
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Jhedzkie,
I think that is happening because you aren't explicitly committing or closing the statement or connection. The driver doesn't know you are done. It is good practice to close resources in a finally block regardless so I would recommend adding that.


Jeanne Boyarsky wrote: A prepared statement should have "?" objects for the parameters...



thanks. that fixed the code.

and also, i will try to rewrite my code according to your suggestion of using "?".

thanks very much
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic