This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Finding the primary key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Finding the primary key" Watch "Finding the primary key" New topic

Finding the primary key

Rik Sweeney
Ranch Hand

Joined: Sep 04, 2001
Posts: 36
I need to allow a user to perform joins on primary/foreign key pairs. Is there anyway I can find a foreign key in a table and it's matching primary key table? I really need the columns from both the tables aswell. Is there an easy way to do this?
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

DatabaseMetaData.getCrossReference should do the trick for you. If your driver does not implement this method then you might have to hit the database system tables to get the info which are database specific.
also DatabaseMetaData.getImportedKeys() and DatabaseMetaData.getExportedKeys() could be useful too.
I agree. Here's the link:
subject: Finding the primary key
It's not a secret anymore!