在mysql中快速插入百万测试数据&&将百万数据写入本地Demo

在mysql中快速插入百万测试数据

public class BaseDao {
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //  获取数据库连接对象
    public static Connection getConn() {
        Connection conn = null;
        try {
            //  rewriteBatchedStatements=true
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/**?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=true", "**", "**");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    //  释放资源
    public static void closeAll(AutoCloseable... autoCloseables) {
        for (AutoCloseable autoCloseable : autoCloseables) {
            if (autoCloseable != null) {
                try {
                    autoCloseable.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
public static void main(String[] args) {
        long start = System.currentTimeMillis(); 
        Connection conn = BaseDao.getConn();
        String sql = "insert into ** values(?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql); 
            //  不断产生sql
            for (int i = 0; i < 6000000; i++) {
                ps.setString(1, i + "");
                ps.setInt(2, i);
                ps.setInt(3, i);  
                ps.addBatch();  //  将一组参数添加到此 PreparedStatement 对象的批处理命令中。
            }
            int[] ints = ps.executeBatch();//   将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。
            //  如果数组长度不为0,则说明sql语句成功执行,即百万条数据添加成功!
            if (ints.length > 0) {
                System.out.println("已成功添加6百万条数据!!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(conn, ps);
        }
        long end = System.currentTimeMillis();
        System.out.println("所用时长:" + (end - start) / 1000 + "秒");
    }

将百万数据写入本地

600多w行实测2s左右

    File f = new File("xxx.txt");
    OutputStreamWriter writer = null;
    BufferedWriter bw = null;
    try {
        OutputStream os = new FileOutputStream(f);
        writer = new OutputStreamWriter(os);
        bw = new BufferedWriter(writer);
        bw.write(str);
        bw.flush();
    }catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值