jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Problem in Using Update Query in PreparedStatment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problem in Using Update Query in PreparedStatment" Watch "Problem in Using Update Query in PreparedStatment" New topic

Problem in Using Update Query in PreparedStatment

Nagendra Batchu
Ranch Hand

Joined: Jan 23, 2008
Posts: 32
Hi All,

I am facing a problem in Using Update query in prepared statement. I am trying to update a huge set of records by using Prepared Statement in JAVA

The Code is as follows:

statement5=connection.prepareStatement("update CHANNELINVENTORY set CHANNELCODE=?,LASTUPDATEON=sysdate where UPPER(ITEMCODE)=? and UPPER(SERIALNUMBER)=? ");
for(int j=0;j<channelInventoryDetails.size();j++){
String channelDetails=(String)channelInventoryDetails.get(j);
String[] channel=channelDetails.split("-");
if (j > 0 && j % 1000 == 0){
System.out.println("before addbatch-from if->"+j);
System.out.println("before addbatch-from else->"+j);

In my present case i am trying to update around 3000 records, in my code after every 1000 records i am executing that batch , which is taking huge time ...nothing is coming its just hanging for huge time .. its just prints this ------------------->before addbatch-from if->1000 thats it

And in the DB there primary key constaints on both the (ITEMCODE+SERIALNUMBER) and there is a index on CHANNELCODE..

Please suggest me the changes...

Is this the right way to update huge data in the table ...or is there any other way to update huge data.

Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

The CHANNELCODE index does not help for this query. It will cost:
- you don't use it in the where clause, so the index is not relevant to speed up the query.
- you are updating CHANNELCODE, so the index has to be updated every time you execute the update.

-- so far for CHANNELCODE.

I don't know if these columns have an index.
But if they have, they will probably not be used, because you use UPPER() around them.

-- so far for ITEMCODE and SERIALNUMBER.

Next 1000:
Is your rollback tablespace big enough to deal with 1000 changes to your database at a single time?
Try your code with committing faster first (say: 2 records).
If that works, you confirmed that your logic is ok, and you can try to increase to a workable amount.
If possible: Commit after an update. This will free up your rollback segment.

Regards, Jan

OCUP UML fundamental and ITIL foundation
Nagendra Batchu
Ranch Hand

Joined: Jan 23, 2008
Posts: 32
Thanks fo your valuable response ..I will try removing the indexes on the column..Thanks a lot
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

Nagendra Batchu wrote:T..I will try removing the indexes on the column..
Oops, that was not what I was trying to suggest. I was explaining why it adds costs in this case. The other suggestions are more important.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

How long does this query take to run once? If it's a non trivial amount of time, tuning it will immensely help your update. This is what I think Jan was getting at with the above post.

Note that some databases like Oracle have "function based indexes" where you can add an index on UPPER(SERIALNUMBER).

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem in Using Update Query in PreparedStatment
It's not a secret anymore!