Java(随机往mysql中导入一亿条数据)

本文介绍如何使用Java程序高效地将一亿条数据批量导入到MySQL数据库中,涉及数据库连接、批量插入及性能优化等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要导入mysql驱动包

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;

public class User {
    private String url = "jdbc:mysql://localhost:3306/test";
    private String user = "root";
    private String password = "root";

    @org.junit.Test
    public void Test(){
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rt = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,user,password);
            String sql = "INSERT INTO user(Name,Sex,Age,Tel) VALUES(?,?,?,?)";
            pstm = conn.prepareStatement(sql);

            conn.setAutoCommit(false);
            Long startTime = System.currentTimeMillis();

            for (int n = 1;n <= 1000;n++){
                Random  rand = new Random();

                for (int i = 1;i <=100000;i++){
                    int a = rand.nextInt(20);
                    int b = rand.nextInt(10);
                    int c = rand.nextInt(10);
                    int d = rand.nextInt(10);

                    int upCase = rand.nextInt(26)+65;//得到65-90的随机数
                    int downCase = rand.nextInt(26)+97;//得到97-122的随机数
                    String up = String .valueOf((char)upCase);//得到A-Z
                    String down = String .valueOf((char)downCase);//得到a-z

                    pstm.setString(1,up+down);
                    pstm.setString(2,"男");
                    pstm.setInt(3, a);
                    pstm.setInt(4, a+5+b+c+2+d);
                    pstm.addBatch();
                }
                pstm.executeBatch();
                conn.commit();
                System.out.println("已经存入"+n*100000+"条");
            }

            Long endTime = System.currentTimeMillis();
            System.out.println("用时"+(endTime-startTime));

        } catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            if(pstm != null){
                try{
                    pstm.close();
                } catch(SQLException e){
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if(conn != null){
                try{conn.close();
                } catch(SQLException e){
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }   
    }
    public static void main(String[] args) {
        User u = new User();
        u.Test();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值