Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get the ID number with AUTO INCREMENT

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent!!!

thanks - this is what i needed.
 
Sean Sullivan
Ranch Hand
Posts: 427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic