Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://" +
"localhost:3306/excel2mysql", "wanle", "wanle");
// 关闭事务自动提交
con.setAutoCommit(false);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
TimeZone t = sdf.getTimeZone();
t.setRawOffset(0);
sdf.setTimeZone(t);
Long startTime = System.currentTimeMillis();
PreparedStatement pst = (PreparedStatement) con.prepareStatement("insert into test04 values (?,'中国')");
for (int i = 0; i < 10000; i++) {
pst.setInt(1, i);
// 把一个SQL命令加入命令列表
pst.addBatch();
}
// 执行批量更新
pst.executeBatch();
// 语句执行完毕,提交本事务
con.commit();
Long endTime = System.currentTimeMillis();
System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
pst.close();
con.close();
Java在数据库操作中批量插入数据
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.