向数据库添加10W条数据

package com.hairoutech.wms.core.sql_test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

public class Test {
private String sql="INSERT INTO db_test (reserved_field1,reserved_field2) VALUES (?,?)";
private String charset="utf-8";
private String connectStr="jdbc:mysql://localhost:3306/wms_core";
private String username="root";
private String password="root";


private void doStore() throws ClassNotFoundException, SQLException, IOException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectStr, username,password);
conn.setAutoCommit(false); // 设置手动提交
int count = 0;
PreparedStatement psts = conn.prepareStatement(sql);
Date begin=new Date();
for(int i=0;i<=100000;i++){
psts.setString(1, i+"reserved_field1");
psts.setString(2, i+"reserved_field2");
psts.addBatch(); // 加入批量处理
count++;
}
psts.executeBatch(); // 执行批量处理
conn.commit(); // 提交
Date end=new Date();
System.out.println("数量="+count);
System.out.println("运行时间="+(end.getTime()-begin.getTime()));
conn.close();
}
public static void main(String[] args) {
try {
new Test().doStore();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

向数据库添加10W条数据

上一篇:JS_0009:微信中隐藏菜单项


下一篇:小程序:登录(获取用户信息、登录)