aspose file tools*
The moose likes JDBC and the fly likes Obtaining auto created id upon finishing insert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Obtaining auto created id upon finishing insert" Watch "Obtaining auto created id upon finishing insert" New topic
Author

Obtaining auto created id upon finishing insert

D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

I'm working on a customer service system which return a customer a ticket number after placing a request. A ticket number is an id in request table, like:

table request
id number
request string
subject string
customer_id number

I use insert like:

I'd like to know ticket id after insertion. Will it be reliable in multi requests environment if I use for getting id under the same transaction the following query:


Is there any more elegant way to do that?


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

In JDBC 3, you can use Statement.RETURN_GENERATED_KEYS to have the database return the values. For older versions of JDBC, your solution is best.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Cool, thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Obtaining auto created id upon finishing insert