This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes Update in For loop. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Update in For loop." Watch "Update in For loop." New topic
Author

Update in For loop.

es kranthi
Ranch Hand

Joined: Dec 27, 2010
Posts: 30
Hi,
I just wanted to know whether writing update SQL query inside a for loop will effect the performance of the Database or not.Since everytime we iterate and update the record.Hence we are updating the Database this may increase the load on the Database hence the performance.

Ex:

Sample code please donot conside syntax

Thanks and Regards.
Sudheer Bhat
Ranch Hand

Joined: Feb 22, 2011
Posts: 75
Check out PreparedStatement and executeBatch of Statement
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Well, yes, if you update a table then that is going to cause the database to do some work. And obviously that means the load on the database is increased, because now it has to do some work that it wasn't doing before. But that's trivially obvious and clearly not what you were looking for.

Unfortunately it's hard to tell what you are looking for instead. Based on your post there isn't much more that can be said.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

You should use batching. The database can be more efficient if you give it more records at the same time because it makes less trips to the database, less commit statements, etc.


[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
Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79
if you get the employee id's list from the database table, then you can also look into using SP instead of update inline SQL


SCJP 1.4, SCWCD 1.4
 
wood burning stoves
 
subject: Update in For loop.