views:

311

answers:

2

I am writing a Java Application. I have got a ResultSet. Now I want to find out the coloumn name of the primary key of the table.

Is it possible to get that coloumn name through ResultSet object or ResultSetMetaData Object or any other way.

I didn't find any way to find this.

+2  A: 

No. You will not get that information from ResultSet or ResultSetMetadata.

What you want to use for that is DatabaseMetadata class. From that class check getPrimaryKeys method to get the information you want.

Of course, to use this, you will need to know the name of the table.

Pablo Santa Cruz
What does "catalog" and "schema" means? Is "schema" referring to the database name?
Yatendra Goel
@Yatendra, what DB are you using? Those are common in DB2. If you use eclipse, and connect with the "Database Development" view, you'll be able to figure it out on your own.
ericp
@ericp, I am using Eclipse IDE and MySQL database. But I don't want my code to be dependent on any particular database.
Yatendra Goel
................................................................
Yatendra Goel
A quick google search, found http://www.tutorialized.com/view/tutorial/Database-Metadata-with-JDBC/13753 - :Sometimes, for a given problem, there are different solutions based on the database vendor. For example, the code that gets the table names for an Oracle database is different from the code that gets the tables names for a MySQL database. When you develop an application or framework for a relational database, be sure that your connection pool manager takes the vendor name as a parameter."
ericp
A: 

A nice tool which you can use to verify the metadata information is dbVisualizer.

It uses the JDBC metadata to retrieve table definitions and other parts of the database. Schema and Catalog are columns in the table definition view - so you can check which values are in these columns for your favorite database.

dbVisualizer is available in a free basic version.

mjustin