JDBC 3.0 adds the function of Automatically Obtaining database automatic fields, such as MySQL autoincreament and SQL Server identity.
// Step 1, add the parameter to create the statement
Preparedstatement pstat = con. preparestatement (
"Insert into t_method (class_id, name, detail, version_from, define) values (?,?,?,?,?) ", Statement. return_generated_keys );
// Perform the update operation
Pstat.exe cuteupdate ();
// Step 2: Obtain the generated primary key
Rs = pstat. getgeneratedkeys ();
Rs. Next ();
// Step 3: Read
String key = Rs. getstring (1); // It can also be obtained by Rs. getstring ("ID ").
Static final int return_generated_keys
This constant indicates that the generated key should be available for retrieval.
Resultset getgeneratedkeys () throws sqlexception
Obtain all automatically generated keys created by executing this statement object. If no key is generated for this statement object, an empty resultset object is returned.
Note: If you do not specify a column that indicates the auto-generated key, the JDBC driver determines the column that best represents the auto-generated key.
Return Value:
A resultset object that contains the keys automatically generated by executing this statement object