File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Get Primary Key

 
Drew Lane
Ranch Hand
Posts: 296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Drew Lane
Ranch Hand
Posts: 296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic