• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

return autogenerated key

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic