dcsimg
How to use PreparedStatement for batch executipn
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sabu_vs
Posted On:   Tuesday, June 4, 2002 01:20 AM

Hi



I have 3 sql insert statements that insert data in 3 different tables.

I want to use batch update fascility using preparedStatent.

But I donot know how to do it.Could you tell me how to do it



Also tell me which is the best for batch updation-PreparedStatement or statement?


thanks


sabu

Re: How to use PreparedStatement for batch executipn

Posted By:   Seshagiri_Kamalakaran  
Posted On:   Wednesday, June 5, 2002 02:41 AM


Hi,


First of all U have to understand that batchUpdate

is mainly used when we have numerous quantity

of Insert,Update,Create...


Inserting only few rows using batch Update doesnt

improve the performance.


If u are having only 3 insert statements then go

for executeUpdate instead of executeBatch


Then for ur second question,It depends upon what

query u r processing

If U r having query with IN parameters then go for

PreparedStatement.executeBatch or otherwise go for the

Statement.executeBatch.


The code snippet for Statement batch Update

con.setAutoCommit(false);

Statement stmt = con.createStatement();

stmt.addBatch("INSERT ...");

stmt.addBatch("INSERT ...");

stmt.addBatch("INSERT ...");

stmt.addBatch("INSERT ...");

int [] updateCounts = stmt.executeBatch();


The code snippet for PreparedStatement batch Update

con.setAutoCommit(false);

PreparedStatement pst= con.prepareStatement("INSERT...")

pst.setString(1,"A");

pst.setString(2,"B");

pst.addBatch();

pst.setString(1,"C");

pst.setString(2,"D");

pst.addBatch();

int[] updateCounts = pst.executeBatch();



Hope this helps U.



With Warm Regards

Seshagiri.K

About | Sitemap | Contact