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

getting info about primary and foreign keys

padmanabh dongre
Greenhorn

Joined: Aug 13, 2001
Posts: 24
Hi
I want to get information about primary keys and foreign keys of a table.can this be done? and how?
i have tried getExportedKeys , getImportedKeys , getCrossReference methods in DatabaseMetaData class. but it is returning an empty resultset. i have used the references keyword to create tables in oracle. this is the code -
import java.sql.*;
public class meta {
public static void main( String cmd[] ) {
try {
Class.forName( "oracle.jdbc.driver.OracleDriver" );
Connection conn = DriverManager.getConnection( "dburl" ,"usernm" ,
"pass");
DatabaseMetaData dm = conn.getMetaData( );
ResultSet rs = dm.getExportedKeys( "" , "" , "table1" );
while( rs.next( ) ) {
System.out.println("inside while for rs");
String pkey = rs.getString("PKCOLUMN_NAME");
String ptab = rs.getString("PKTABLE_NAME");
String fkey = rs.getString("FKCOLUMN_NAME");
String ftab = rs.getString("FKTABLE_NAME");
System.out.println("primary key table = " + ptab);
System.out.println("primary key = " + pkey);
System.out.println("foreign key table = " + ftab);
System.out.println("foreign key = " + fkey);
}
ResultSet rs2 = dm.getCrossReference( null , null , "table1" ,
null , null , "table2" );
while( rs2.next( ) ) {
System.out.println("inside while for rs2");
String pkey = rs.getString("PKCOLUMN_NAME");
String ptab = rs.getString("PKTABLE_NAME");
String fkey = rs.getString("FKCOLUMN_NAME");
String ftab = rs.getString("FKTABLE_NAME");
System.out.println("primary key table = " + ptab);
System.out.println("primary key = " + pkey);
System.out.println("foreign key table = " + ftab);
System.out.println("foreign key = " + fkey);
}
ResultSet rs3 = dm.getImportedKeys( null , null , "table1" );
while( rs3.next( ) ) {
System.out.println("inside while for rs3");
String pkey = rs.getString("PKCOLUMN_NAME");
String ptab = rs.getString("PKTABLE_NAME");
String fkey = rs.getString("FKCOLUMN_NAME");
String ftab = rs.getString("FKTABLE_NAME");
System.out.println("primary key table = " + ptab);
System.out.println("primary key = " + pkey);
System.out.println("foreign key table = " + ftab);
System.out.println("foreign key = " + fkey);
}
}
catch( Exception e ) {
e.printStackTrace( );
}
}
}
Please help,
Thanx.
Padmanabh
Rahul Shinde
Greenhorn

Joined: Jan 29, 2002
Posts: 9
Hi padmanabh
Check ur code ,
atleast dm.getImportedKeys works fine
and returns the results
Instead of rs3, u have used "rs". (also for rs2)
bye
Rahul
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I had this problem with oracle at one time too. I believe I used all capital letters and it solved the problem.

see if it works for you too.
Jamie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting info about primary and foreign keys