Steps:
1) set the primary key to be returned. [use the second parameter of preparedstatement]
Preparedstatement preparestatement (string SQL, int autogeneratedkeys); // creates a default preparedstatement object that can obtain automatically generated keys.
Parameters:
Autogeneratedkeys-Indicates whether to return the automatically generated key flag,
It is one of statement. return_generated_keys or statement. no_generated_keys.
2) Execute SQL statements
Pstmt.exe cuteupdate (); // other methods are also supported
3) obtain all automatically generated keys created by executing this statement object
Resultset getgeneratedkeys ()
4) obtain the generated primary key
While (Rs. Next ()){
System. Out. println ("primary key value:" + Rs. getint (1 ));
}
Code:
/* You can return the generated primary key */pstmt = Conn. preparestatement (sqlmapping. add_user_primarysql, statement. return_generated_keys);/* Execute SQL statement */INT rows = pstmt.exe cuteupdate ();/* obtain the key value generated by the executed SQL statement */rs = pstmt. getgeneratedkeys (); // generatedkeys: The generated key while (RS. next () {system. out. println ("primary key value:" + Rs. getint (1 ));}
Obtain automatically generated primary keys