• 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

updating fields with prepared statement and looping

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic