File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Getting unique ID for INSERT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Getting unique ID for INSERT" Watch "Getting unique ID for INSERT" New topic

Getting unique ID for INSERT

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362

I have a small problem I have come accross, which I can't see an obious way round.

I have a RegistrationServlet (for creating a new user) which inserts the details of the new user into two seperate tables in a MS SQL database.

The first insert puts the users personal data into a table called tblUsers (name, dob, tel no, address, etc). The second insert puts the users security details into a table called tblUserSecurity (username, password, etc).

The problem I have is, the only way to uniquely identify the user is from the ID column (from tblUsers) which is set as the primary key and automatically created for each record added, I need to somehow take this value for the new user and insert it into the tblSecurity table along with the username, password etc as a way of linking the user to their security details.

Any help would be greatly appreciated.

praveen kanakarajan
Ranch Hand

Joined: Apr 27, 2005
Posts: 54
im not a great expert.i think u can get the value of last identity by using the query
select @@identity
will return the latest identity value.Just try this im not sure.but hope will help you
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Cheers for that, but I have found a way to get the last record eneterd (I think), but tanks anyway. Always nice to have a plan 'B'.
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
You may have solved this differently, but if I understand you problem, you could use something along the lines of:

Hope this helps (someone!)
I agree. Here's the link:
subject: Getting unique ID for INSERT
It's not a secret anymore!