PreparedStatement preparestatement (String sql,int autogeneratedkeys) throws SQLException
Autogeneratedkeys can be evaluated as Statement.return_generated_keys or Statement.no_generated_keys
When you take the Statement.return_generated_keys value and use an INSERT statement, you can remove the value of the automatically growing column in the newly inserted data row, for example:
1 BooleanAutocommit =conn.getautocommit ();2Conn.setautocommit (false);3 4 intRootid =-1;//Rootid the same value as the first column field, the first column field is autogrow5String sql = "INSERT into article values (NULL,?,?,?,?, now (),?)";6PreparedStatement pstmt =DB.CREATEPSTMT (conn, SQL, Statement.return_generated_keys);7Pstmt.setint (1, 0);8Pstmt.setint (2, Rootid);9Pstmt.setstring (3, Request.getparameter ("title"));TenPstmt.setstring (4, Request.getparameter ("cont")); OnePstmt.setint (5, 0); A pstmt.executeupdate (); - -ResultSet Rskey =Pstmt.getgeneratedkeys (); the Rskey.next (); -Rootid = Rskey.getint (1);//Remove the value of the first column field -Statement stmt =DB.CREATESTMT (conn); -Stmt.executeupdate ("Update article Set Rootid =" + Rootid + "WHERE id =" + rootid);//Update + -Conn.commit ();//atomicity of a transaction +Conn.setautocommit (autocommit);//Recovery Site
Applicable occasions:
When you insert a field column in the database with an auto-growing column, assign a value of-1 to column, use the overloaded method to remove the field value for the autogrow column, and then execute the UPDATE statement to renew the column field.
About records for Preparestatement (String sql,int Autogeneratedkeys)