JDBC to obtain automatically generated Field Values

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.