Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to retrieve the new ID of the auto_increment column

Status
Not open for further replies.

metridevkk

Programmer
Sep 13, 2004
4
US
I am using mysql database.
In my jsp page, after inserting a record in the mysql table, I need to get back the new ID generated for the auto_increment primary key column. How do I get this value?

 
Does mysql use the @@IDENTITY variable?

If so, it would be:
SELECT @@IDENTITY;

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks. I found the solution as follows:

stmt.executeUpdate(insertSQL,Statement.RETURN_GENERATED_KEYS);

int autoIncKeyFromApi = -1;

rs = stmt.getGeneratedKeys();

if (rs.next()) {
autoIncKeyFromApi = rs.getInt(1);
} else {
// throw an exception from here
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top