Thursday, December 22, 2011

Batch Update Using Statement with MySql Database

BatchExecuteStatement.java
------------------------------------------------------------------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class BatchExecuteStatement {

    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");
       Statement st=con.createStatement();
       st.addBatch("insert into employee values(101,'raj')");
       st.addBatch("insert into employee values(102,'satyam')");
       st.addBatch("insert into employee values(103,'wipro')");
       st.addBatch("insert into employee values(104,'suman')");
      
      int count[]=st.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 in Classpath

No comments:

Post a Comment