日期:2014-05-16 浏览次数:20517 次
ConnDB cd = new ConnDB(); Connection ct = null; PreparedStatement pst = null; ResultSet rs = null; ct = cd.getConn(); String insertSql = "insert into mytable (name,age) values(?,?)"; System.out.println(insertSql); try { ct.setAutoCommit(false); // 开始事务 pst = ct.prepareStatement(insertSql); for (int i = 0; i < 120000; i++) { pst.setString(1, "name" + i); pst.setInt(2, i); pst.addBatch(); System.out.println(i); //分段提交 if((i%50000==0&& i!=0)||i== (120000 -1)){ pst.executeBatch(); ct.commit(); ct.setAutoCommit(false);// 开始事务 pst = ct.prepareStatement(insertSql); System.out.println("------------>50000"); } } ct.commit();// 提交事务 } catch (SQLException e) { try { ct.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } catch (RuntimeException e) { try { ct.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { cd.close(ct, pst, rs); } }
ConnDB cd = new ConnDB(); Connection ct = null; PreparedStatement pst = null; ResultSet rs = null; ct = cd.getConn(); String deleteSql = "delete from mytable t where t.name=?"; System.out.println(deleteSql); int[] intArray = new int[120000]; try { ct.setAutoCommit(false); // 开始事务 pst = ct.prepareStatement(deleteSql); for (int i = 0; i < 120000; i++) { pst.setString(1, "name" + i); pst.addBatch(); System.out.println(i); // 分段提交 if ((i % 50000 == 0 && i != 0) || i == (120000 - 1)) { intArray = pst.executeBatch(); ct.commit(); ct.setAutoCommit(false);// 开始事务 pst = ct.prepareStatement(deleteSql); System.out.println("------------>50000"); } } ct.commit();// 提交事务 } catch (SQLException e) { try { ct.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } catch (RuntimeException e) { try { ct.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { cd.close(ct, pst, rs); } }
ConnDB cd = new ConnDB(); Connection ct = null; PreparedStatement pst = null; ResultSet rs = null; ct = cd.getConn(); String deleteSql = "update mytable t set t.age =20 where t.name=?"; System.out.println(deleteSql); int[] intArray = new int[120000]; try { ct.setAutoCommit(false); // 开始事务 pst = ct.prepareStatement(deleteSql); for (int i = 0; i < 120000; i++) { pst.setString(1, "name"+i); pst.addBatch(); System.out.println(i); // 分段提交 if ((i % 50000 == 0 && i != 0) || i == (120000 - 1)) { intArray = pst.executeBatch(); ct.commit(); ct.setAutoCommit(false);// 开始事务 pst = ct.prepareStatement(deleteSql); System.out.println("------------>50000"); } } ct.commit();// 提交事务 } catch (SQLException e) { try { ct.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } catch (RuntimeException e) { try { ct.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { cd.close(ct, pst, rs); } }