BatchExecutePreparedStatment.java
----------------------------------------------------------------------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class BatchExecutePreparedStatment {
public static void main(String[] args)throws Exception
{
String userName = "root";
String password = "root";
String url = "jdbc:mysql://localhost/dhportaldb";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection con = DriverManager.getConnection (url, userName, password);
con.setAutoCommit(false);
PreparedStatement pst=con.prepareStatement("insert into employee values(?,?)");
pst.setInt(1,100);
pst.setString(2,"raj");
pst.addBatch();
pst.setInt(1,200);
pst.setString(2,"satyam");
pst.addBatch();
pst.setInt(1,300);
pst.setString(2,"wipro");
pst.addBatch();
int count[]=pst.executeBatch();
con.commit();
for(int i=0;i<count.length;i++)
if(count[i]!=0)
{
con.rollback();
break;
}
}
}
ClassPath:Add mysql-connector-java-5.0.8-bin.jar to your classpath
----------------------------------------------------------------------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class BatchExecutePreparedStatment {
public static void main(String[] args)throws Exception
{
String userName = "root";
String password = "root";
String url = "jdbc:mysql://localhost/dhportaldb";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection con = DriverManager.getConnection (url, userName, password);
con.setAutoCommit(false);
PreparedStatement pst=con.prepareStatement("insert into employee values(?,?)");
pst.setInt(1,100);
pst.setString(2,"raj");
pst.addBatch();
pst.setInt(1,200);
pst.setString(2,"satyam");
pst.addBatch();
pst.setInt(1,300);
pst.setString(2,"wipro");
pst.addBatch();
int count[]=pst.executeBatch();
con.commit();
for(int i=0;i<count.length;i++)
if(count[i]!=0)
{
con.rollback();
break;
}
}
}
ClassPath:Add mysql-connector-java-5.0.8-bin.jar to your classpath
No comments:
Post a Comment