时间:2021-07-01 10:21:17 帮助过:1人阅读
1 //批量写入数据测试 2 public void InsertBatch(List<T> list){ 3 String sql = "insert into t(go,back) values(?,?)"; 4 DBHelper dbh = new DBHelper(sql); 5 Connection conn = dbh.returnConn(); 6 try { 7 conn.setAutoCommit(false);//注意此句一定要为false,原因见第一篇参考文献 8 PreparedStatement ps = conn.prepareStatement(sql); 9 for(int i = 0; i < list.size(); i++){ 10 ps.setInt(1, list.get(i).getI()); 11 ps.setInt(2, list.get(i).getJ()); 12 ps.addBatch(); 13 if (i % 10000 == 0){ 14 ps.executeBatch(); 15 conn.commit(); 16 } 17 } 18 ps.executeBatch(); 19 conn.commit(); 20 conn.close(); 21 } catch (SQLException e) { 22 // TODO 自动生成的 catch 块 23 e.printStackTrace(); 24 } 25 }
数据表:
实验结果:
【实践】jdbc批量插入数据
标签: