This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JDBC and the fly likes Can i batch preparedStatements? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Can i batch preparedStatements?" Watch "Can i batch preparedStatements?" New topic
Author

Can i batch preparedStatements?

Alok Pota
Ranch Hand

Joined: Mar 07, 2001
Posts: 185
I would like to use the batching mechanismof JDBC 2.0 on preparedStatements. Can it be done?
Sample code?..
PreparedStatement ps = conn.preparedStatement(sql);
ps.setInt(1,35);
ps.setInt(2,67);
ps.addBatch(); <-------- Not sure where this goes
hanumanth reddy
Ranch Hand

Joined: Jun 12, 2000
Posts: 118
The folliwng code is correct

PreparedStatement ps = conn.preparedStatement(sql);
ps.setInt(1,35);
ps.setInt(2,67);
ps.addBatch();
ps.setInt(1,40);
ps.setInt(2,76);
ps.addBatch();
ps.executeBatch();


<a href="http://www.jobklub.com" target="_blank" rel="nofollow">http://www.jobklub.com</a><br /> 'Add Job To Life'
Steve Chernyak
Ranch Hand

Joined: Oct 19, 2000
Posts: 113
What if you have different statements that need to be executed?
For example:
insert into parent (id, field) values (?, ?);
insert into child (parent_id, anotherField) values (?, ?);
I am able to get the batch to work with Statements, is there anyway to do this with PreparedSatements?
Thanks
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
It may work. I had something similar work on one driver but not the other, it may be the JDBC version they support. Anyway, try something like this:

[ July 11, 2003: Message edited by: Lu Battist ]
Steve Chernyak
Ranch Hand

Joined: Oct 19, 2000
Posts: 113
I will have to do some performance testing to see if this is faster than using plain statements. I was hoping to send all of the sql to the database in one batch, but it might be faster to send multiple batches using PreparedStatements. I guess it will depend on wether it takes longer to send the data over the network or for the database to parse the sql.
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can i batch preparedStatements?
 
Similar Threads
Two instances of PreperedStatment
Filtering Data to prevent SQL Injection
how to display a SQL query ?
inserting an image in to database
dealing with ' -- an apostrophe