Get all primary keys in database.

T

tolu45

Hi, I am writing a code to retrieve all the primary keys of the tables
in the database. I tried to use
DatabaseMetadata dbmd = dbConnection.getMetaData();
ResultSet rs = dbmd.getprimarykeys(null,null ,"%"),

I do not get the result set in my vector which i created to store the
primarykeys. But when i explicitly write the name of the table like
ResultSet rs = dbmd.getprimarykeys(null,null, "movies"), i get the
primary key. DO someone know why i have this problem?
 
M

Michael Rauscher

tolu45 said:
Hi, I am writing a code to retrieve all the primary keys of the tables
in the database. I tried to use
DatabaseMetadata dbmd = dbConnection.getMetaData();
ResultSet rs = dbmd.getprimarykeys(null,null ,"%"),

I do not get the result set in my vector which i created to store the
primarykeys. But when i explicitly write the name of the table like
ResultSet rs = dbmd.getprimarykeys(null,null, "movies"), i get the
primary key. DO someone know why i have this problem?

Yep, you don't have a table named "%".

Bye
Michael
 
E

EricF

Yep, you don't have a table named "%".

Bye
Michael

Michael is right. The JDBC implemetation is building a select statement to
query the DB catalog. '%' is only used as a wild card when used with a LIKE
clause. (There may be some implementations where % would work - but I'd be
surprised.)

Eric
 
L

Lee Fesperman

EricF said:
Michael is right. The JDBC implemetation is building a select statement to
query the DB catalog. '%' is only used as a wild card when used with a LIKE
clause. (There may be some implementations where % would work - but I'd be
surprised.)

Actually, certain methods in java.sql.DatabaseMetaData will accept one or more arguments
containing wildcards (LIKE pattern), such as, getTables(). See the docs. However,
getPrimaryKeys() does not, which was Michael's point.

To the OP: the solution is to use getTables() to get a set of tables and then invoke
getPrimaryKeys() on each table from that set.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,134
Latest member
Lou6777736
Top