日期:2014-05-20 浏览次数:20947 次
Connection conn = null; PreparedStatement pstmt = null; try { conn = DriverManager.getConnection(XXX,XXX,XXX); conn.setAutoCommit(false); [color=#FF0000]pstmt = conn.prepareStatement("INSERT INTO XXXX");//执行插入操作 1[/color] pstmt.addBatch(); //省略中间代码 [color=#FF0000]pstmt.addBatch(sql1);//执行修改操作 2[/color] pstmt.addBatch(sql2);//执行修改操作 pstmt.addBatch(sql3);//执行修改操作 pstmt.executeBatch(); conn.commit(); } catch (Exception e) { e.printStackTrace(); try { if (conn != null){ [color=#FF0000] conn.rollback();//此处被调用 3[/color] } } catch (SQLException e1) { e1.printStackTrace(); } }finally{ if(pstmt != null) try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); pstmt = null; } if(conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); conn = null; } }