aspose file tools*
The moose likes JDBC and the fly likes How to Get Primary Key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to Get Primary Key" Watch "How to Get Primary Key" New topic
Author

How to Get Primary Key

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I just performed an INSERT within my JSP page, but I need to return the value of the primary key which was generated by the database to the next JSP page.
I tried to perform another query within the same connection, but I'm having problems (Before start of result set, etc.)
What's the easiest way to do this?
Thanks,
Drew
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
as for the first question, read this post:
http://www.coderanch.com/t/297502/JDBC/java/know-last-auto-increment-number
I don't know if this is what you want to achieve, but it might guide you...
for the second error:
when you get an error like "Before start of result set" it means that after you perform a query your result set is placed one position before the first row that has been retrieved. So if you want to access the resultset you have to do something like this FIRST:
result.next()
now you are located in the first row and can access it.
hope it helps


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Hey, Thanks - that was exactly what I want.
I was able to get this to work with:
LAST_INSERT_ID()
I'm not sure it's ideal since it only works with MySQL, AFAIK.
Did you find a more portable solution?
Drew
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
as you read on my post, I think I was complicating myself. As David suggested me, I did not use the database to generate my unique ID. I used System.currentTimeMillis() and played with this number a little bit. Now all the files that I have uploaded so far have a unique name.
not the best solution... i guess... but it's pretty straighforward..
cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Get Primary Key