Saturday, March 21, 2009

jdbc batch update



import java.sql.*;
class BatchUpdateDemo
{
public static void main(String[] args)throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","chandru");
System.out.println(" Connected 2 DB.....");

con.setAutoCommit(false);
Statement stmt=con.createStatement();
String vsql1="insert into emp2 values(1231,'chandu',100)";
String vsql2="insert into emp2 values(1224,'chandu',100)";
String vsql3="insert into emp2 values(1233,'chandu',100)";

try
{
stmt.clearBatch();
stmt.addBatch(vsql1);
stmt.addBatch(vsql2);
stmt.addBatch(vsql3);
stmt.executeBatch();
con.commit();//without commit the data won't be effected in DB.
}
// any of the above addBatch fail whole transaction rooBack
catch(Exception e)
{
con.rollBack();
}
System.out.println("Data has been entered successfully. ....");

}
}



0 comments:

Post a Comment