wood burning stoves 2.0*
The moose likes JDBC and the fly likes updating fields with prepared statement and looping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "updating fields with prepared statement and looping" Watch "updating fields with prepared statement and looping" New topic
Author

updating fields with prepared statement and looping

Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

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

Joined: Oct 28, 2009
Posts: 118
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
 
subject: updating fields with prepared statement and looping
 
Similar Threads
need help with Tomcat
java.io.IOException Too many open files error
Converting Hex to decimal - with a twist
here is a question and i have a very similar code but i need to convert it
where this program goes wrong(using 2D array)?