直接上代码
:
public class pretInsertTest {
/*
* 批量插入时间测试
*/
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
Long startTime = System.currentTimeMillis();
try {
Class.forName(Config.drivername);
conn = DriverManager.getConnection(Config.url_ike,Config.username,Config.password);
// 关闭事务自动提交
conn.setAutoCommit(false);
// 开始时间
String sql = "insert into test (name,age,sex,num,time) values(?,?,?,?,?)";
String time = DateTimeUtil.getCurrDateTimeStrNew();
ps = conn.prepareStatement(sql);
for(int i = 0;i<500000;i++){
ps.setString(1,"隔壁老王");
ps.setInt(2,20);
ps.setString(3,i%2 == 0 ?"男":"女");
ps.setInt(4,i);
ps.setString(5,time);
ps.addBatch();
if(i%1000 == 0){
ps.executeBatch();
conn.commit();
System.out.println("来一波");
}
}
ps.executeBatch();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ps.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Long endTime = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
TimeZone tz = sdf.getTimeZone();
tz.setRawOffset(0);
sdf.setTimeZone(tz);
System.out.println("用时:"+sdf.format(new Date(endTime - startTime)));
}
}