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

inserted primary key

 
shankar vembu
Ranch Hand
Posts: 309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is there any way to get the primary key of my newly inserted record.
I tried using the statement with RETURN_GENERATED_KEYS flag set but did not quite get what i wanted. Is this the way to do it?
Regards,
SHankar.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which database are you using?
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
There is an example here
----------------
Sainudheen
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep
Originally posted by Pradeep Bhat:
Which database are you using?

Is it database dependent?
----------------
Sainudheen
 
Mani Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:
Which database are you using?

While the ability to generate primary keys is database dependent, the retrieval of the generated primary keys using JDBC is not.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mani Ram:

While the ability to generate primary keys is database dependent, the retrieval of the generated primary keys using JDBC is not.

Not all database support autogenerated keys.
The method DatabaseMetaData.supportsGetGeneratedKeys returns true if
a JDBC driver and underlying data source support the retrieval of automatically
generated keys.
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I got it.
--------------
Sainudheen
 
Mani Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:
Not all database support autogenerated keys.

That's exactly what I said!
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mani Ram:

That's exactly what I said!

The key cannot be retrieved if database does not support it.
I do not think Oracle support retrieval of autogenerated keys. Most of the cases the drivers are dependent on database (type 4 and type 2)
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only does the Database have to support it, but the JDBC Driver has to support it as well.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
Not only does the Database have to support it, but the JDBC Driver has to support it as well.

You mean to say that is a must for datbase to support autogenerated key?
Then why do we need DatabaseMetaData.supportsGetGeneratedKeys () method
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And so.. if the Driver supports JDBC 3.0 and Database support these keys we can use this feature. Am I correct?
----------------
Sainudheen
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sainudheen Mydeen:
And so.. if the Driver supports JDBC 3.0 and Database support these keys we can use this feature. Am I correct?
----------------
Sainudheen

Sainu
I agree with you.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sainudheen Mydeen:
And so.. if the Driver supports JDBC 3.0 and Database support these keys we can use this feature. Am I correct?
----------------
Sainudheen

That is correct.
 
shankar vembu
Ranch Hand
Posts: 309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys. just had the time today to try this out and modify my code. it works well with my MySQL driver.
Regards,
shankar
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic