aspose file tools*
The moose likes JDBC and the fly likes Auto-generated ID number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Auto-generated ID number" Watch "Auto-generated ID number" New topic
Author

Auto-generated ID number

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Hi,

I am using JavaDB (aka Apache Derby). I have a table like this: -


I would like to insert a row into this database and find the auto-generated Token value for the inserted row. It is not obvious how I would do that using JDBC.

Any suggestions?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30913
    
158

Peter,
In JDBC 3.0, this is easy to do. See this Derby manual page for details.


[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
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
How easy is it in JDBC 2?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30913
    
158

Marilyn,
Some databases provided database specific methods to get the generated keys. Alternatively, you could do a query based on the data you entered (assuming it was distinct) to get the key.

Note that JDBC 3 came out with Java 1.4. So it's been a while since I did it "the old way".
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Thanks, Jeanne
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Jeanne Boyarsky:
In JDBC 3.0, this is easy to do. See this Derby manual page for details.


Thanks. This looks like exactly what I want.

FWIW I got the functionality I needed using the Derby-specific function IDENTITY_VAL_LOCAL(). But the getGeneratedKeys() method looks much better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Auto-generated ID number