MYSQL批量插入数据

@Test
public void test(){
long time1 = new Date().getTime();
int count = 0;
String sql = "insert into ip_table(ip_add,country,time) values(?,?,?)";
try {
conn.setAutoCommit(false);        //如何没有设置此参数,则要435秒,设置之后只有11秒
pst = conn.prepareStatement(sql);
} catch (SQLException e1) {
e1.printStackTrace();
}
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
count ++;
String ip = "24.91."+i+"."+j;
System.out.println(count);
    try {
    pst.setString(1, ip);
    pst.setString(2, "US");
    pst.setString(3, DateUtil.getStringCurrentDateTime());
    pst.addBatch();
    if(count % 1000 == 0){ //如果1000改为5000或10000,时间则为10秒
    pst.executeBatch();
    conn.commit();
    pst.clearBatch();        //此行可以不要,速度一样
    }
    } catch (SQLException e) {  
    e.printStackTrace();  
    }
}
}
try {
pst.executeBatch();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
long time2 = new Date().getTime();
System.out.println("用时:"+(time2-time1)/1000+" 秒");

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值