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 ()