aspose file tools*
The moose likes JDBC and the fly likes Need a sample query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Need a sample query" Watch "Need a sample query" New topic
Author

Need a sample query

Deepthi Xavy
Ranch Hand

Joined: Aug 31, 2009
Posts: 32
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

Joined: Feb 19, 2008
Posts: 2902
    
    1



[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


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

Joined: Jan 01, 2009
Posts: 874

Sagar Rohankar wrote:


Sagar , Dont just throw the code. let them work out with your hints.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Jan 01, 2009
Posts: 874


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


Thats true and what i meant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need a sample query