This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes executeBatch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "executeBatch" Watch "executeBatch" New topic
Author

executeBatch

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Has anyone done batch sql statements?
I have a bunch of insert statements that will make up an order. I want all the statements to run as one unit, just like a batch, and if any of them fails, it rollsback.
So I was wondering that if setAutoCommit(false) is called on a connection, then run executeBatch on the statement object, which will ahve all the insert statements there. Then check to see if all executed, and if not rollback, otherwise commit.
Is this the way to go, or is there a better way that anyone knows of?
Thanks
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well looking at Sun press's JDBC book. I found that batch is the way I want to go, I can get transactions in it, and it returns an array of ints if it is completely successful, and throws a BatchUpdateException if any one of the statements fail.
thanks
Mark
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
If you are using Oracle, then you can control the number of rows when to do a commit.
You can either do it at the connection level:
((OracleConnection) conn).setDefaultExecuteBatch(n);
or at the PreparedStatement level:
((OraclePreparedStatement) pstmt).setExecuteBatch(n)


Normal is in the eye of the beholder
 
jQuery in Action, 2nd edition
 
subject: executeBatch