使用jdbc批量插入数据

使用jdbc 批量插入数据,速度也不是很快1万条数据大概需要4.5秒左右

package com.example.studyspringboot.studyboot.utils.stuMysqlBatchInsert;

import com.example.studyspringboot.studyboot.utils.jdbc.DBUtils;
import org.junit.Test;

import java.sql.*;

//大批量数据插入
//参考文档https://www.cnblogs.com/myseries/p/11191134.html
public class InsertBatch {
    @Test
    public void method() {
        System.out.println((int)(Math.random()*10000));
    }

    @Test
    public void insertBatch() throws ClassNotFoundException {
        //思路:将100万条数据分成n等份,1等份为1000条数据
        //如何实现?
        //1、必须将Connection接口的自动提交方式改为手动
        //2、利用Statement接口中的如下三个方法:addBatch、clearBath、executeBatch
        Connection conn;
        PreparedStatement pstm;
        ResultSet rt = null;
        try {

            conn =  DBUtils.getConn("jdbc:mysql://localhost:3306/redistest?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai","root","lps123");

            conn.setAutoCommit(false);
            String sql = "INSERT INTO user(name,sex,hobby,height) values(?,?,?,?)";
            pstm = conn.prepareStatement(sql);

            conn.setAutoCommit(false);//关闭自动提交
            int random;
            Long startTime = System.currentTimeMillis();

            Long headTime=startTime;
            long tailTime;
            for (int i = 1; i <= 1000025; i++) {//100万用了近8分钟(443秒,一万条数据大概4.5秒)
                random= (int)(Math.random()*10000);
                pstm.setString(1,i+"_name_"+random);
                pstm.setString(2, i+"_sex_"+random);
                pstm.setString(3, i+"_hobby_"+random);
                pstm.setString(4, i+"_height_"+random);
                pstm.addBatch();
                if((i%10000)==0){
                    tailTime=System.currentTimeMillis();
                    pstm.executeBatch();//执行插入
                    conn.commit();
                    pstm.clearBatch();
                    System.out.println("每插入一万条所用时间:"+(tailTime-headTime));
                    headTime=tailTime;
                }
            }
            pstm.executeBatch();//执行插入
            conn.commit();
            pstm.clearBatch();
            Long endTime = System.currentTimeMillis();
            System.out.println("总用用时:" + (endTime - startTime)+"ms");
            DBUtils.close(conn,pstm,null);//关闭资源

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

DBUtil

package com.example.studyspringboot.studyboot.utils.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {

    //1. 封装获取连接的方法
    public static Connection getConn(String url,String user,String password) throws ClassNotFoundException, SQLException {
        //注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接对象//url="jdbc:mysql://localhost:3306/tempwork" ;user = "root"  ;password = "lps123";
        Connection conn = DriverManager.getConnection(url,user, password);
        //返回连接对象
        return conn;
    }

    //2. 封装关闭资源的方法
    public static void close(Connection conn,
                             Statement stat, ResultSet rs) {

        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (stat != null) {
                stat.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) {
                conn.setAutoCommit(true);
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值