aspose file tools
The moose likes JDBC and Relational Databases and the fly likes return autogenerated key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "return autogenerated key" Watch "return autogenerated key" New topic

return autogenerated key

gaurav chaudhary
Ranch Hand

Joined: Jun 24, 2002
Posts: 64
I have two tables....The autogenerated key of the first one which is also the primary key is the foreign key in the second one. Can u please tell me how do i get the id generated from the first table when I insert the record in it
thanks in advance

Irene Loos
Ranch Hand

Joined: Apr 15, 2002
Posts: 78
You can have second query that reads immidiately after insert. Put in this query parameters that you used to generate the record. It should bring you back your id.

Irene Loos
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

some of the new drivers that implement the JDBC 3.0 interface have a Statement.getGeneratedKeys() method. Otherwise, you are looking at doing something like mentioned above or create your own autogenerated key using a millisecond date representation( long datatype ) as the key.
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The preferred way would be JDBC 3.0 getGeneratedKeys(), but usually there is also database-specific syntax to get the last key generated in the current transaction; eg SELECT @@IDENTITY on SQL Server. You would execute this as you would any SQL query.
- Peter
Have you tried LearnNowOnline?
subject: return autogenerated key