The moose likes JDBC and the fly likes identity Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "identity" Watch "identity" New topic
Author

identity

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
Dan
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')");
Jamie
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: identity
 
Similar Threads
Newbie Question
what is identity in object
No entity without identity
Are instances of enums static by default?
auto-increament in ms sql server2000