jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes Getting the Keys in a Resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Getting the Keys in a Resultset" Watch "Getting the Keys in a Resultset" New topic
Author

Getting the Keys in a Resultset

Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hello All,

Good Day...

I want to write a function that will determine what are the keys of a particular table.

I have a query string that will use "select * from @table_name" where @table_name is determined at runtime.

Given the resultset from this query string, i need to determine which is the primary keys in this table. Is it possible to know this?

I googled for some answer and found the class ResultSetMetaData which is useful to some extent to determine the properties of each columns but this does not answer all my needs.

Please advice if I could accomplish this. Thanks.


Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

Many databases of meta data tables that you can query. Think of it as tables about tables. The format/storage of them is non-standard though so check your specific database system documentation.


My Blog: Down Home Country Coding with Scott Selikoff
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Scott,

Thanks for the reply..

I forgot to mention in my post that we are connecting to the database by remote and have limited access on system database tables that holds the data about the tables.

We are just given a set or a list of tables that we need to query to determine its Keys and display it. So I started looking up at the Java API for this but the class ResultSetMetaData was the one I found out. Unfortunately theres no method that would return if the column is a key or part of the keys.

Perhaps there's no workaround for this but to ask the DB Admin to give me access on these tables. Thanks again.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

Why would the user of the system ever need to know about key relationships in the database? Better yet, why would they be likely to change? Sounds a web database anti-pattern to me.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Getting the Keys in a Resultset
 
Similar Threads
Difference between rowid and rownum in sql query??
Prepared statement
Oracle describe table from jdbc
ResultSet is closed??
Database Tables