This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes JDBC addBatch problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC addBatch problem" Watch "JDBC addBatch problem" New topic

JDBC addBatch problem

Jaime Garcia
Ranch Hand

Joined: Jan 07, 2003
Posts: 38
Hi. I am working with DB2 7.2 on NT. I use a preparedStatement to insert a lot of records so I use the addBatch statement. The performance is good when using addBatch but in some rows a column is changed :|, I mean, supouse record 1 as
column1_1 column1_2 column1_3
and record 2 as
column2_1 column2_2 column2_3
after the inserts using batch I got somting like
column1_1 column2_2 column1_3
column2_1 column2_2 column2_3
It happends with some rows only. If I use prepStmt.executeUpdate() instead of prepStmt.addBatch() it works ok, but slower
Any Idea ?.
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30130

Odd. It's almost like the value isn't being set. Could you post the loop to set the batch? Maybe it isn't storing that column?

[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
Jaime Garcia
Ranch Hand

Joined: Jan 07, 2003
Posts: 38
The section where I asign the values is:

The column that has the problem is:
stmt.setString(4, (String) nombreTrab.trim());
I am sure that it is asigned becouse if I change the stmt.addBatch() to stmt.executeUpdate() it works. A friend told me that he has the same problem with char columns, JDBC type 3 (net) from IBM DB2 7, I am not sure but it seems to be a bug on the driver. He told me that they had to contact IBM support, maybe I'll try this.
Jaime Garcia
Ranch Hand

Joined: Jan 07, 2003
Posts: 38
I am not sure if I was wrong or IBM driver. I used the batch like this:

so that each 10000 rows process the batch and continue with next rows.
I changed it to:

And it works. The JDBC API dose not say that you have to close a PreparedStatement after executing a batch. Maybe is a bug on DB2 JDBC, maybe i did not read all.
Loren Rosen
Ranch Hand

Joined: Feb 12, 2003
Posts: 156
Do you have autocommit enabled or disabled? If you have it enabled (the default) I'd try disabling it and adding some explicit commits. If you can, try committing every individual update, and then try committing every 10,000 update.
[ March 17, 2004: Message edited by: Loren Rosen ]
Jaime Garcia
Ranch Hand

Joined: Jan 07, 2003
Posts: 38
I am using autocommit off. I do not think that the problem could be with commit becouse if I execute the statement executeBatch() after adding all batch (addBatch) I do not have any problem.
But if I use addBatch() (100 times, for example), executeBatch() and, with the same statement (with out closing the statemet) clearBatch(), addBatch() (100 time, for example) and again executeBatch() The problem apears.
If I close the statement after each executeBatch() I got no problem, like in the first case, using executeBatch() only at the end.
Don't get me started about those stupid light bulbs.
subject: JDBC addBatch problem
Similar Threads
how to insert null in numeric field
Q: repeatedly execute() on PreparedStatement without close()
Fastest way to insert 1 Lac records in a table
Problem in using batchs
prepared statement in for loop