• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

get the ID number with AUTO INCREMENT

 
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?
 
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.
 
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
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!