Possible Duplicate:
How to get the insert ID in JDBC?
I have the following tables:
customer(cid, ...)
book(cid, rid)
reservation(rid, ...)
I want to add a reservation in its table then I want to add in a relationship in book between customer and reservation.
How can I know rid while it is auto generated, and when I execute the query I did get a ResultSet returned?
The rid is auto generated form Oracle using a sequence and a trigger.
Refer to your question I believe you are using statements. In this case, you can use
Statement.RETURN_GENERATED_KEYS. Also please consider this link as below solution can have conficts for Oracle database.Here is small example:
in case of you are using ORM:
If you are using JPA
entitiyManager.merge(entity);(or update) should return persisted instance with ID.Quite similar with hibernate
session.merge(entity);(or update)