Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need a sample query

 
Deepthi Xavy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • 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 Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic