Eight: using JDBC batch

First, the use Statement batch processing

  1, using the Statement object is added to a batch execute SQL statements, as follows:

1 Statement.addBatch(sql1);
2 Statement.addBatch(sql2);
3 Statement.addBatch(sql3);

  2, execute a batch of SQL statements: Statement.executeBatch ();
  3, Clear batch commands: Statement.clearBatch ();

1.1, using the example of a batch is completed Statement

  1, the preparation of test SQL script to create the table

1  create table testbatch
2 (
3      id int primary key,
4      name varchar(20)
5 );

  2, preparation of test code, as follows:

Package me.gacl.demo; 

Import the java.sql.Connection;
 Import the java.sql.ResultSet;
 Import the java.sql.Statement;
 Import me.gacl.utils.JdbcUtils;
 Import org.junit.Test; 

/ ** 
* @ClassName : JdbcBatchHandleByStatement 
* @Description: Statement implemented using JDBC batch operations 
* @author : aloof Wolf 
* @date: 2014-9-20 PM 10:05:45 
* 
* /  
public  class JdbcBatchHandleByStatement { 

    @Test 
    public  void testJdbcBatchHandleByStatement () { 
        Conn Connection = null ; 
        of Statement ST= null;
        ResultSet rs = null;
        try{
            conn = JdbcUtils.getConnection();
            String sql1 = "insert into testbatch(id,name) values(1,'aaa')";
            String sql2 = "insert into testbatch(id,name) values(2,'bbb')";
            String sql3 = "insert into testbatch(id,name) values(3,'CCC')";
            String sql4 = "insert into testbatch(id,name) values(4,'DDD')";
            String sql5 = "update testbatch set name='gacl' where id=1";
            Sql6 String= "INSERT INTO testbatch (the above mentioned id, name) values (5, 'FFF')" ; 
            String sql7 = "testbatch the Delete from the WHERE the above mentioned id = 2" ; 
            ST = conn.createStatement ();
             // add bulk to execute SQL 
            ST .addBatch (SQL1); 
            st.addBatch (SQL2); 
            st.addBatch (SQL3); 
            st.addBatch (sql4); 
            st.addBatch (sql5); 
            st.addBatch (sql6); 
            st.addBatch (SQL7); 
            // executing batch SQL statements 
            st.executeBatch ();
             // Clear batch command 
            st.clearBatch (); 
        } the catch (Exception E) {
            e.printStackTrace();
        }finally{
            JdbcUtils.release(conn, st, rs);
        }
    }
}

1.2, using Statement.addBatch (sql) way to achieve the advantages and disadvantages of the batch

  Using Statement.addBatch (sql) way to achieve the batch:
    advantages: you can send a number of different SQL statements to the database.
    Drawback: SQL statement is not pre-compiled.
    When sending multiple statements to the same database, but only parameters different SQL statements, to be repeated on many write SQL statements. E.g:

1 Insert into user(name,password) values('aa','111');
2 Insert into user(name,password) values('bb','222');
3 Insert into user(name,password) values('cc','333');
4 Insert into user(name,password) values('dd','444');

Second, the use PreparedStatement batch processing

2.1, use PreparedStatement batch processing paradigm

  Test code is as follows:

Package me.gacl.demo; 

Import the java.sql.Connection;
 Import java.sql.PreparedStatement;
 Import the java.sql.ResultSet;
 Import me.gacl.utils.JdbcUtils;
 Import org.junit.Test; 

/ ** 
* @ClassName : JdbcBatchHandleByStatement 
* @Description: prepareStatement implemented using JDBC batch operations 
* @author : aloof Wolf 
* @date: 2014-9-20 PM 10:05:45 
* 
* /  
public  class JdbcBatchHandleByPrepareStatement { 

    @Test 
    public  void testJdbcBatchHandleByPrepareStatement () {
         Long startTime = System.currentTimeMillis();
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        
        try{
            conn = JdbcUtils.getConnection();
            String sql = "insert into testbatch(id,name) values(?,?)";
            st = conn.prepareStatement(sql);
            for(int i=1;i<1000008;i++){  //i=1000  2000
                st.setInt(1, i);
                st.setString(2, "aa" + i);
                st.addBatch();
                if(i%1000==0){
                    st.executeBatch();
                    st.clearBatch();
                }
            }
            st.executeBatch();
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            JdbcUtils.release(conn, st, rs);
        }
        long endtime = System.currentTimeMillis();
        System.out.println("程序花费时间:" + (endtime-starttime)/1000 + "秒!!");
    }
}

2.2, using PreparedStatement.addBatch () way to achieve the advantages and disadvantages of the batch

  Using PreparedStatement.addBatch () batch processing
    advantages: sent after the pre-compiled SQL statement, implementation of high efficiency.
    Disadvantages: can only be used in SQL statements the same, but different batch parameters. Thus this form is often used for bulk batch insert data in the same table, or data table bulk update.

Guess you like

Origin www.cnblogs.com/deityjian/p/11695644.html