This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Auto-generated ID number

 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
In JDBC 3.0, this is easy to do. See this Derby manual page for details.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How easy is it in JDBC 2?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jeanne
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic