1. 程式人生 > >JDBC進行批處理的四種方式

JDBC進行批處理的四種方式

這裡以MySQL資料庫為例來展示四種通過jdbc實現批處理的四種方法,這裡只舉例了插入100條資料的寫法,其他大致相同。

第一種:最簡單,最好理解的(不要這樣寫)
(沒工作經驗可能會這樣寫,執行效率最低)

public class jdbcDemo1 {
    public static void main(String[] args) throws Exception{
        Connection con = null;
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("url"
, "user", "password"); PreparedStatement ins = null; Date date = new Date(); System.out.println(date.getTime()); for (int i = 1; i <= 100; i++) { String sql = "insert into t_test (id,user) values (?,?)"; ins = con.prepareStatement(sql); ins.setInt(1
, i); ins.setInt(2, i); ins.execute(); } Date dateTwo = new Date(); System.out.println(dateTwo.getTime()); } }

第二種:使用事務,不自動commit(常用的吧)

public class jdbcDemo1 {
    public static void main(String[] args) throws Exception{
        Connection con = null
; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("url", "user", "password"); PreparedStatement ins = null; int COMMIT_SIZE = 200; con.setAutoCommit(false); PreparedStatement ins = null; Date date = new Date(); System.out.println(date.getTime()); for (int i = 101; i <= 200; i++) { String sql = "insert into t_test (id,user) values (?,?)"; ins = con.prepareStatement(sql); ins.clearParameters(); ins.setInt(1, i); ins.setInt(2, i); ins.execute(); if (i % COMMIT_SIZE == 0) { con.commit(); } con.commit(); } Date dateTwo = new Date(); System.out.println(dateTwo.getTime()); } }

第三種: executeBatch(推薦)

public class jdbcDemo1 {
    public static void main(String[] args) throws Exception{    
        Connection con = null;
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("url", "user", "password");
        PreparedStatement ins = null;   

        int COMMIT_SIZE = 100;
        int BATCH_SIZE = 100;
        String sql = "insert into t_test1 (id,data) values (?,?)";
        con.setAutoCommit(false);
        PreparedStatement ins = null;
        ins = con.prepareStatement(sql);

        Date date = new Date();
        System.out.println(date.getTime());

        for (int i = 101; i <= 200; i+= BATCH_SIZE) {
            ins.clearBatch();
            for (int j = 0; j < BATCH_SIZE; j++){
                ins.setInt(1, i+j);
                ins.setInt(2, j);
                ins.addBatch();
            }
            ins.executeBatch();
            if ((i + BATCH_SIZE - 1) % COMMIT_SIZE == 0) {
                con.commit();     
            } 
        }
            con.commit();

            Date dateTwo = new Date();
            System.out.println(dateTwo.getTime());
    }
}

第四種:先load再commit(速度最快)
(這樣寫最快。但是有些小瑕疵,sql不安全,需要改寫一部分)
為什麼不安全是因為這個sql是拼出來的,要改成之前寫的(?,?)就可以了。

public class jdbcDemo1 {
    public static void main(String[] args) throws Exception{    
        Connection con = null;
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("url", "user", "password");
        PreparedStatement ins = null;   

        int COMMIT_SIZE = 400;
        PreparedStatement ins =null;
        con.setAutoCommit(false);

        Date date = new Date();
        System.out.println(date.getTime());

        ins = con.prepareStatement("load data local infile '' "
                    + "into table t_test fields terminated by ','");
        StringBuilder sb = new StringBuilder();
        for (int i = 304; i <= 404; i++) {
            sb.append(i + "," + i + "\n");
            if(i % commitSize == 0){
                InputStream is=new ByteArrayInputStream(sb.toString()
                                .getBytes());
                ((com.mysql.jdbc.Statement) ins)
                                .setLocalInfileInputStream(is);
                ins.execute();
                con.commit();
                sb.setLength(0);
            }
        }
        InputStream is=new ByteArrayInputStream(sb.toString().getBytes());
        ((com.mysql.jdbc.Statement) ins).setLocalInfileInputStream(is);
        ins.execute();
        con.commit();

        Date dateTwo = new Date();
        System.out.println(dateTwo.getTime());
    }
}