How do I get the automatically growing ID primary key value in the MSSQL database table in the JSP?

Source: Internet
Author: User
Tags mssql stmt

How do I get the automatically growing ID primary key value in the MSSQL database table in the JSP?

For example: to build a table student have attribute columns Userid,username where UserID is a primary key with an int type read-only automatically plus 1 (that is, each inserted record automatically adds 1), then how to get the UserID value of the current inserted row in the JSP (for example: I inserted three records into the table, and when I inserted the third record, the value of UserID should be 3, so how do I get this 3?


To create a stored procedure first:
CREATE PROCEDURE Addrec
(
@OutID int OUTPUT,
@Name varchar (25)
)
As
Declare @ID int
Insert into NameTable (Name)
VALUES (@Name)
Select @ID =@ @IDENTITY
Select @OutID = @ID
Go

And then use this:
CallableStatement Stmt=con.preparecall ("{Call Addrec (?,?)}");
Stmt.registeroutparameter (1,types.integer,1);
Stmt.setstring (2, "Name.");
Stmt.execute ();
int Id=stmt.getint (1);
Stmt.close ()


Related Article

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.