码迷,mamicode.com
首页 > 其他好文 > 详细

用PreparedStatement批量插入数据

时间:2021-05-03 12:52:05      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:etc   obj   自动提交   tar   tin   nts   state   com   dstat   

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);
        }
    }
}

用PreparedStatement批量插入数据

标签:etc   obj   自动提交   tar   tin   nts   state   com   dstat   

原文地址:https://www.cnblogs.com/getheading/p/14724334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!