Java快速向数据库中插入大量数据 比如10万条以上
1 String sql = "insert into table *****";
2 //必须要有这句,要不然效果不明显
3 con.setAutoCommit(false);
4 ps = con.prepareStatement(sql);
5 for(int i=1; i<102030; i++){
6 ps.addBatch();
7 // 1w条记录插入一次
8 if (i % 10000 == 0){
9 ps.executeBatch();
10 con.commit();
11 }
12 }
13 // 最后插入不足1w条的数据
14 ps.executeBatch();
15 con.commit(); 1 String sql = "insert into table *****"; 2 //必须要有这句,要不然效果不明显 3 con.setAutoCommit(false); 4 ps = con.prepareStatement(sql); 5 for(int i=1; i<102030; i++){ 6 ps.addBatch(); 7 // 1w条记录插入一次 8 if (i % 10000 == 0){ 9 ps.executeBatch(); 10 con.commit(); 11 } 12 } 13 // 最后插入不足1w条的数据 14 ps.executeBatch(); 15 con.commit();
下一篇:
推荐国内靠谱得技术社区
