File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting info about primary and foreign keys

 
padmanabh dongre
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic