• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can i batch preparedStatements?  RSS feed

 
Alok Pota
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Steve Chernyak
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!