This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes How do I get auto-generated key by DB2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How do I get auto-generated key by DB2" Watch "How do I get auto-generated key by DB2" New topic

How do I get auto-generated key by DB2

Suresh Yadla

Joined: Mar 26, 2005
Posts: 12
I am inserting a row into a table in DB2 and DB2 provides an auto-generated key into the primary key column.

After the insert I am retreiving that value by "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1", but the problem is how do I know that this is the ID that DB2 has generated for the above insert.. How do I ensure that no one else has done an insert and if someone else has done an insert then the IDENTITY_VAL_LOCAL() returns the ID of the second insert...

I am using JDBC 2.0 so I know that I cannot use, RETURN_GENERATED_KEYS in the preparedStatement. Is there any possible way that I can ensure that no one does a second insert until the IDENTITY_VAL_LOCAL() returns a value ?

Please help me out if you know, I would really appreciate your help.


Suresh Yadla<br />SCJP, SCWCD, SCBCD<br /> <br />----------------------------------
I agree. Here's the link:
subject: How do I get auto-generated key by DB2
It's not a secret anymore!