File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Databases » JDBC
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
 
Consider Paul's rocket mass heater.
 
subject: identity