Using JDBC to get the SQL Autogrow ID

Source: Internet
Author: User

In the project development, encountered a problem, first add a record and then want to immediately get the ID value of this record, the ID is automatically increased by SQL Server, if you first insert the query, you need to execute another Query ID SQL statement, so the following method:

1. Use the identity function that comes with SQL Server:

sql= "INSERT INTO dbo. Obdcustomer VALUES (?,?,?,?,?,?) SELECT @ @IDENTITY as ID ";

2. To execute this statement, it should be a query, not an update, so you need to call the ExecuteQuery () method

3, the implementation of the problem encountered, prompted this method did not return the result set, the online find a way, in front plus set NOCOUNT on:

Sql= "SET NOCOUNT on insert to dbo. Obdcustomer VALUES (?,?,?,?,?,?) SELECT @ @IDENTITY as ID ";

4. Execute again, return the ID value successfully

Note:

There are other methods on the Internet, such as obtaining through statement, stored procedures to obtain, etc., personally feel that the above identity method is relatively simple, do not know whether the other database is supported.

Using JDBC to get the SQL Autogrow ID

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.