0

I'm currently writing code that inserts new values into a relation that has an auto-generated ID.

create table person (
person_id   number(5) generated always as identity
            minvalue 1
            maxvalue 99999
            increment by 1 start with 1
            cycle
            cache 10,
firstname   varchar(10) not null,
lastname    varchar(10) not null,
);

Then, I'm trying to insert a tuple within Java with JDBC:

Statement s;
String query = "insert into PERSON (firstname, lastname) values (\'" + firstname + "\'" + ", " + "\'"
                    + lastname + "\')";
s.executeUpdate(query);

This all works fine, but I need to get the generated ID back into my Java program so that I can work with it. Is there any way to do this well?

Thanks!

ac_nook
  • 325
  • 1
  • 2
  • 11

1 Answers1

0

Use the JDBC method stmt.getGeneratedKeys(). That will give you back the IDs just generated.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • That in itself is not sufficient to get the generated id. You also need to prepare / execute the statement in the right way. – Mark Rotteveel Apr 20 '18 at 07:35