Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) 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
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!