向数据库批量处理事件

 1 Connection conn;
 2 Class.forName("com.mysql.jdbc.Driver");
 3 conn = DriverManager.getConnection(
 4 "jdbc:mysql://88.88.88.88:3306/zmm?useUnicode=true&characterEncoding=UTF-8", "root", "root");
 5 conn.setAutoCommit(false);
 6 // 保存当前自动提交模式
 7 boolean autoCommit = conn.getAutoCommit();
 8 // 关闭自动提交
 9 conn.setAutoCommit(false);
10 Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
11 
12  for (String key : map.keySet()) {
13 
14 //编辑
15 
16 stmt.addBatch("update zmm set id= '" + zmm+ "',zmm = '" + zmm+ "',status = 0 where id ='"+id+"'");
17 
18 //插入
19 
20 //stmt.addBatch("insert into zmm set id = '"+id+"', create_date = '"+sdf.format(date)+"', cut_off_date = null, zmm= '" + zmm+ "',status = 0");
21 
22 //list.size()或者map.size()都可以点size方法
23 
24 if (i % 1000 == 0 || i == list.size()) {
25 try {
26 stmt.executeBatch();
27 stmt.clearBatch();
28 conn.commit();
29 } catch (Exception e) {
30 conn.close();
31 e.printStackTrace();
32 }
33 
34 i = i + 1;
35 
36 }
37 
38 //关闭事件
39 
40 conn.close();
41  
原文地址:https://www.cnblogs.com/zmmfeng/p/10008726.html