• 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

Need a sample query

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai friends,

Consider a case that if I want to insert 60 records of student, for their attendance. I just need to do bulk insert of all 60records at a second.Can any one get a sample query.Even if there is chance for batch update how to do it,can any one get me a sample querry.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Consider a case that if I want to insert 60 records of student, for their attendance. I just need to do bulk insert of all 60records at a second.Can any one get a sample query.Even if there is chance for batch update how to do it,can any one get me a sample querry.




Use PreparedStatement with '?' placeholders on how many columns you have. And setautocommit as true
Have each student record available as JavaBean and add it a collection (preferably ArrayList)
In a While or for loop (iterate ArrayList) , fetch each record and execute preparedstatement created code.
This way , even if it fails at 40 th record , you would have 39 records committed successfuly.

Use Batch update only when (in your case) you think all the individual inserts/updates combines to make a single transaction.

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sagar Rohankar wrote:



Sagar , Dont just throw the code. let them work out with your hints.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I reread DontBeACodeMill again just to ensure that whether I disobeyed any JR suggestion and I'm happy to knew that I haven't (unless someone more experienced Rancher correct me ) !!
This is the particular statement which make my sample code to be reasoned.

That being said, the best approach is often to show enough code to point the questioner in the right direction.



Next thing, when I see the OP is looks like a little confused about whether to use Batch statement or simple for loop for inserting the records, I thought this time just let her know how the simple code looks like for the same.

And what you explained in your first reply is same as the few lines of code of mine.(Yes asked the OP to use transactions and explain the difference between the batch and loop )
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And what you explained in your first reply is same as the few lines of code of mine



Thats true and what i meant.
reply
    Bookmark Topic Watch Topic
  • New Topic