This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Returning a sequence id Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Returning a sequence id" Watch "Returning a sequence id" New topic

Returning a sequence id

Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi all

I am using JDBC to insert a row into a table and the id for that row is created using a sequence. Is there any way to get that sequence number right after you execute or a way to have that value returned.


Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
Hi Andrew,

Which database are you using..? For informix, you can do something like this.

Check your jdbc provider API, there might be something similar to this. Since this is database dependent, it is not part of standard JDBC API and beware you will lose portability if you use this.


SCJP 1.4
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Sorry I am using Oracle 10.2. I am trying select seq.nextVal from dual now and it seems to be working. Is this method thread safe?


Jan Cumps

Joined: Dec 20, 2006
Posts: 2543

.. sequence.nextval ... Is this method thread safe?

Yes. Oracle guarantees a unique id.

Regards, Jan

OCUP UML fundamental and ITIL foundation
I agree. Here's the link:
subject: Returning a sequence id
It's not a secret anymore!