This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes JDBC and the fly likes identity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "identity" Watch "identity" New topic


jesse harris
Ranch Hand

Joined: Oct 02, 2000
Posts: 62
anyone know a good way to return the identity of a row that was just inserted?
Daniel Dunleavy
Ranch Hand

Joined: Mar 13, 2001
Posts: 276
if you are talking about MS SQL SERVER....
you should call a stored procedure which does the insert and the last statement in the procedure is
select @@IDENTITY
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by jesse harris:
anyone know a good way to return the identity of a row that was just inserted?

Another idea that works with oracle(it may be possible in SQLServer too) is to use a select statement to obtain the new identifier. In Oracle the database will give out the next available id (as if it were in an insert statement) then use the id in your insert statement.
Oracle code:
resultset = statement.executeQuery("select sequence.nextVal from dual");
long nextId = resultset.getLong(1);
--> this selects the next
id value from the database. dual is just a dummy table so you can accomplish the select statement.
statement.executeUpdate("insert into table (id, name) values (" + nextId + ", 'Joey Joe Joe Shabadoo')");
I agree. Here's the link:
subject: identity