aspose file tools*
The moose likes JDBC and the fly likes inserted primary key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "inserted primary key" Watch "inserted primary key" New topic
Author

inserted primary key

shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
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

Joined: Feb 27, 2002
Posts: 8919

Which database are you using?


Groovy
Sainudheen Mydeen
Ranch Hand

Joined: Aug 18, 2003
Posts: 218
Hi
There is an example here
----------------
Sainudheen
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

http://www.onjava.com/pub/a/onjava/synd/2001/08/21/jdbc.html?page=3
Sainudheen Mydeen
Ranch Hand

Joined: Aug 18, 2003
Posts: 218
Pradeep
Originally posted by Pradeep Bhat:
Which database are you using?

Is it database dependent?
----------------
Sainudheen
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
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.


Mani
Quaerendo Invenietis
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Aug 18, 2003
Posts: 218
Thanks. I got it.
--------------
Sainudheen
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Originally posted by Pradeep Bhat:
Not all database support autogenerated keys.

That's exactly what I said!
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Jul 11, 2001
Posts: 15299
    
    6

Not only does the Database have to support it, but the JDBC Driver has to support it as well.


GenRocket - Experts at Building Test Data
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Aug 18, 2003
Posts: 218
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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: May 10, 2001
Posts: 309
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
 
 
subject: inserted primary key