在Java中以一定的时间间隔添加大量数据 [英] Add lots of data with some time interval in Java

查看:78
本文介绍了在Java中以一定的时间间隔添加大量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jdbc在一段时间内添加数据库表中的记录.

I want to add records I database table with some interval of time using jdbc.

例如,我想以10秒的间隔添加100000条记录,以便它每秒插入10000条.

For ex., I want to add 100000 records in 10 sec interval so it'll insert 10000/sec.

我的MySQL代码如下:

My code of MySQL as below :

String url1 = "jdbc:mysql://localhost:3306/xyz";
String user = "root";
String password = "root";
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null) {
    System.out.println("Connected to the database xyz");
    for(int i=0;i<=n;i++){ // where n is no. of record that I want to insert
        // Here is my insert logic
    }
}

推荐答案

@ yogesh-jalodara在我的评论中,我的意思是这样的

@yogesh-jalodara In my comments I meant something like that

final long loopDuration = 1;//second
final long totalSize = 100000;
final long timeInterval = 10;
final AtomicLong batchNumber = new AtomicLong((long)Math.ceil((double) timeInterval / loopDuration));
Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        //insert logic
        if (batchNumber.decrementAndGet() == 0) {
            timer.cancel();
            timer.purge();
        }

    }
}, 0, loopDuration * 1000);

这篇关于在Java中以一定的时间间隔添加大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆