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