`

Mysql中返回PreparedStatement执行后影响的记录条数

阅读更多
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);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics