The previous example works with JDBC 3.0. However, my server doesn’t use the 3.0 implementation, so I had to do it by using a stored procedure which returns the key for the inserted row (SCOPE_IDENTITY() value).
Here’s the skeleton for how I did it:
CREATE PROCEDURE YOUR_STORED_PROCEDURE_NAME (
@RowId INT OUTPUT
INSERT INTO YOUR_TABLE [ETC ….] ;
SET @RowId = SCOPE_IDENTITY();