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 get the ID number with AUTO INCREMENT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "get the ID number with AUTO INCREMENT" Watch "get the ID number with AUTO INCREMENT" New topic
Author

get the ID number with AUTO INCREMENT

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
I have a table with the following atributes:

1. ID
2. First Name
3. Last Name
4. Status

The ID is given automatically by the db (mySql) - AUTO INCREMENT

say I have just created a new record and I want to get the ID number - is there anyway to do that?
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
You can use the last_insert_id() or mysql_insert_id() functions. Both of the functions return the id for auto_increment columns.


But there are some minor differences see below:



Get Unique iD from Last Insert
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Excellent!!!

thanks - this is what i needed.
Sean Sullivan
Ranch Hand

Joined: Sep 09, 2001
Posts: 427
Some JDBC drivers support Auto Generated Keys.

I've got an example here:

http://strutsblobapp.sourceforge.net/xref/example/filestorage/DerbyFileStorageDAO.html

Look at the insertNewFile method. The method uses PreparedStatement.getGeneratedKeys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get the ID number with AUTO INCREMENT