日期:2014-05-16 浏览次数:20908 次
boolean autoCommit = conn.getAutoCommit();//手动提交事务,保证数据库数据的一致性。 conn.setAutoCommit(false); int rootid = -1; String sql = "insert into article values(null, ?, ?, ?, ?, now(), ?)"; PreparedStatement pstmt = DB.getPstmt(conn, sql, Statement.RETURN_GENERATED_KEYS); pstmt.setInt(1,0); pstmt.setInt(2,rootid); pstmt.setString(3,title); pstmt.setString(4,cont); pstmt.setInt(5,0); pstmt.executeUpdate(); ResultSet rsKey = pstmt.getGeneratedKeys(); rsKey.next(); rootid = rsKey.getInt(1); Statement stmt = DB.createStmt(conn); stmt.executeUpdate("update article set rootid = " + rootid + " where id = " + rootid); conn.commit(); conn.setAutoCommit(autoCommit);