aspose file tools*
The moose likes JDBC and the fly likes Can I use a single statement while autocommit is false. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Can I use a single statement while autocommit is false." Watch "Can I use a single statement while autocommit is false." New topic
Author

Can I use a single statement while autocommit is false.

Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Hi All,
I want to insert a large set of data to a table. I am following the below process.

con.setAutoCommit(false);
for(int i=0;i<100;i++){
stmt.addBatch();
stmt.addBatch();
stmt.addBatch();
stmt.addBatch();
stmt.executeBatch();
if(m%5==0){
con.commit();
}
}
con.commit();
con.setAutoCommit(true);

I can use the same statement in adding batches. Now if you see I want to use the same statements in a single transaction too. Can I do that? I mean each transaction contains 5 statements to be executed. So is it compulsory that I should use 5 different instances for statements by calling con.createStatement or is it ok to have a single instance(and it is handled by the jdbc itself)?

Hope I have clearly explained my idea.

Thanks in advance.


Regards, Fawad Ali.
Software Engineer, Stafona Inc. - My Blog
Ed Ward
Ranch Hand

Joined: Jan 30, 2006
Posts: 147
No, you do not need to create five different statements when using them in a batch, unless of course the SQL itself is different. For instance, this snippet was taken from the
JDBC 4.0 Specification.




You can do somthing similar with Statement.addBatch(String sql);
The caveat would be to ensure you are calling commit() or rollback() correctly should a BatchUpdateException be thrown.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I use a single statement while autocommit is false.
 
Similar Threads
doing transaction using jdbc
Updates
oracle connection with jdbc
Batch DB Updatation
Transaction error(Invalid cursor state)