• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I use a single statement while autocommit is false.

 
Fawad Ali
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic