Link to home
Start Free TrialLog in
Avatar of John4343
John4343

asked on

Get last ID after identity insert in Derby DB

I have a table in a Derby database with a column created:

Id int GENERATED ALWAYS AS IDENTITY  CONSTRAINT  PK PRIMARY  KEY

How do I get the id of the last record inserted?  I think I need IDENTITY_VAL_LOCAL, but how do I call it from code?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John4343
John4343

ASKER

This took me a while as the link only shows part of ther answer.  The Insert must be done with executeUpdate and the second parameter:

stmt.executeUpdate("INSERT INTO xx(Name) VALUES ('Joe')", Statement.RETURN_GENERATED_KEYS);

ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
      int autoKey = rs.getInt(1); }

Thanks