读书人

java中间的批处理

发布时间: 2012-10-30 16:13:36 作者: rapoo

java当中的批处理

public void updateStateBactch(List elms) { Connection conn = null; PreparedStatement ps = null; String sql = "update test_table set state=? where keyid = ?"; conn = DBTools.getConnection(); if(conn == null) { log.error("[update][state][error][conn is null]"); return; } try { ps = conn.prepareStatement(sql); for(int i = 0; i < elms.size(); i++) { Element elm = (Element) elms.get(i); if(null == elm || null == elm.getUserId() || null == elm.getState()) { continue; } ps.setInt(1, elm.getStatus()); ps.setString(2, elm.getProcID()); ps.addBatch(); } ps.executeBatch(); ps.clearBatch(); } catch (SQLException sqlEx) { log.warn("[update][state][error][SQLException]"); } catch (Exception e) { log.warn("[update][state][error][SQLException]"); } finally { DBTools.close(conn, ps, null); } }

try { ps = conn.prepareStatement(sql); for(int i = 0; i < elms.size(); i++) { Element elm = (Element) elms.get(i); if(null == elm || null == elm.getUserId() || null == elm.getState()) { continue; } ps.setInt(1, elm.getStatus()); ps.setString(2, elm.getProcID()); ps.addBatch(); if ((i != 0 && i % 2000 == 0) || i == elms.size() - 1) { ps.executeBatch(); ps.clearBatch(); ps.close(); ps = conn.prepareStatement(sql); } } } catch (SQLException sqlEx) { log.warn("[update][state][error][SQLException]"); log.warn(sqlEx); } catch (Exception e) { log.warn("[update][state][error][SQLException]"); log.warn(e); } finally { DBTools.close(conn, ps, null); }

try { ps = conn.prepareStatement(sql); for(int i = 0; i < elms.size(); i++) { try { Element elm = (Element) elms.get(i); if(null == elm || null == elm.getUserId() || null == elm.getState()) { continue; } ps.setInt(1, elm.getStatus()); ps.setString(2, elm.getProcID()); ps.addBatch(); if ((i != 0 && i % 2000 == 0) || i == elms.size() - 1) { ps.executeBatch(); ps.clearBatch(); ps.close(); ps = conn.prepareStatement(sql); } } catch (SQLException e) { log.warn("[update][state][error][SQLException]"); log.warn(e); ps.clearBatch(); ps.close(); ps = conn.prepareStatement(sql); } catch (Exception e) { log.warn("[update][state][error][SQLException]"); log.warn(e); ps.executeBatch(); ps.clearBatch(); ps.close(); ps = conn.prepareStatement(sql); } } } catch (SQLException sqlEx) { log.warn("[update][state][error][SQLException]"); log.warn(sqlEx); } catch (Exception e) { log.warn("[update][state][error][SQLException]"); log.warn(e); } finally { DBTools.close(conn, ps, null); }

Int[] results = ps.executeBatch();

读书人网 >软件架构设计

热点推荐