在做论坛新建帖子insert操作过程中,需要帖子的rootid与自动生成的主键id一致,需要使用到Statement的参数RETURN_GENERATED_KEYS。 conn.setAutoCommit(false); sta = conn.prepareStatement("insert into a values(null,?,now())",Statement.RETURN_GENERATED_KEYS); sta.setInt(1,-1); sta.executeUpdate(); rs = sta.getGeneratedKeys(); rs.next(); int key = rs.getInt(1); rs.close(); sta.executeUpdate("update a set rootid=" + key + " where id=" + key); conn.commit(); conn.setAutoCommit(true); sta.close(); sta = null;