wood burning stoves*
The moose likes JDBC and the fly likes return autogenerated key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "return autogenerated key" Watch "return autogenerated key" New topic
Author

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
gaurav chaudharygaurav@yahoo.com


gaurav
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.
Jamie
Peter den Haan
author
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return autogenerated key