• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can I use a single statement while autocommit is false.

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic