aspose file tools*
The moose likes Servlets and the fly likes How to return Primary Key (autonumber) after an Insert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to return Primary Key (autonumber) after an Insert" Watch "How to return Primary Key (autonumber) after an Insert" New topic
Author

How to return Primary Key (autonumber) after an Insert

Tony Huynh
Greenhorn

Joined: Aug 21, 2001
Posts: 7
Hi all,
I am developing an Intranet system. Anyway I'm purely using JSPs to do it. And I have no problem doing the usual SQL commands. However, I have a problem now. I need to return the automatically incremented Primary Key after I insert a new record. I have also developed web apps using ASP before and used the cursor bookmarks. JSP is a tad different so if someone could help me, I'd be forever in their debt!!
Other Information:
I'm using DB2 as my database.
Using Websphere Studio as my development environment.
Using Websphere Test Environment (in VAJ) to run the JSPs.
Thanks in Advance!
-Tony
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15624
    
  15

Unlike ASP, Java code is expected to be able to "run anywhere", which often means not supporting features that are unique to a given vendor's DBMS, such as auto-incrementing keys (I know it's a common feature, but it's not in the SQL standard). So from a purely portable perspective, there's nothing you could latch on to. For a particular vendor's DBMS, however, you may find an extra driver function or the like - check their jdbc docs. Or, if you're using SQL Server, someone else's jdbc docs, since Microsoft doesn't provide a Java driver.
This problem gets even worse when working with Enterprise JavaBeans, where the key is expected to be known BEFORE the record is added - so much so, that I expect Sun will eventually have to provide a rathole for it.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return Primary Key (autonumber) after an Insert
 
Similar Threads
How to return Primary Key (auto increment) after an Insert
how to know the last auto-increment number using JDBC
After insert statement, how do I get column value populated by DB trigger
SQLServerException: The statement must be run before the generated keys are available
Is there a way to get the primary key with just one insert statement?