用PreparedStatement批量插入數據


package com.czf.blob;

import com.czf.util.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * 使用PreparedStatement實現批量數據的操作
 *
 * update、delete本身具有批量操作的效果
 * 此時的批量操作主要指的是批量插入。使用PreparedStatement如何實現更高效的批量操作?
 *
 * 題目:向goods表中插入兩萬條數據
 * 方式一:使用Statement
 */
public class InsertTest {
    //批量插入的方式二:使用PreparedStatement
    @Test
    public void testInsert1() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {

            long start = System.currentTimeMillis();

            connection = JDBCUtils.getConnection();
            String sql = "insert into goods(name) values(?)";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 1; i <= 20000; i++) {
                preparedStatement.setObject(1, "name_" + i);
                preparedStatement.execute();
            }
            long end = System.currentTimeMillis();
            System.out.println("花費的時間為:" + (end - start));//20000:49918
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, preparedStatement,null);
        }
    }

    /**
     * 批量插入的方式三:
     * 1.addBatch()、executeBatch()、clearBatch()
     * 2.mysql服務器默認是關閉批處理的,我們需要通過一個參數,讓mysql開啟批處理的支持
     *              ?rewriteBatchedStatements=true  寫在配置文件url后面
     */
    @Test
    public void testInsert2() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {

            long start = System.currentTimeMillis();

            connection = JDBCUtils.getConnection();
            String sql = "insert into goods(name) values(?)";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 1; i <= 1000000; i++) {
                preparedStatement.setObject(1, "name_" + i);

                //1.“攢”sql
                preparedStatement.addBatch();
                if (i % 500 == 0){
                    //2.執行
                    preparedStatement.executeBatch();//注意是executeBatch(),不是execute()否則就只插入了i/500條數據

                    //3.清空
                    preparedStatement.clearBatch();
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("花費的時間為:" + (end - start));//20000:49918 -- 1153   1000000:17204
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, preparedStatement,null);
        }
    }

    /**
     * 批量插入的方式四:在方式三基礎上設置連接不允許自動提交數據
     */
    @Test
    public void testInsert3() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {

            long start = System.currentTimeMillis();

            connection = JDBCUtils.getConnection();

            //設置不允許自動提交數據
            connection.setAutoCommit(false);
            String sql = "insert into goods(name) values(?)";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 1; i <= 1000000; i++) {
                preparedStatement.setObject(1, "name_" + i);

                //1.“攢”sql
                preparedStatement.addBatch();
                if (i % 500 == 0){
                    //2.執行
                    preparedStatement.executeBatch();

                    //3.清空
                    preparedStatement.clearBatch();
                }
            }
            //提交數據
            connection.commit();
            long end = System.currentTimeMillis();
            System.out.println("花費的時間為:" + (end - start));//20000:49918 -- 1153 1000000:17204 -- 8511
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, preparedStatement,null);
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM