java.sql.DatabaseMetaData metadata = db.getMetaData();//db为一个connection
ResultSet rs = metadata.getExportedKeys(null, null, tablename);//获得某个表的metadata
String foreigntablename = rs.getString(7);//从 1到16 你依次试试,就知道各个的含义了,另外也可以自己直接查api,看是什么...  7是外键的表名,其他有字段名等等等等

解决方案 »

  1.   

    可以用下面的方法:
    1。getPrimaryKeys(String catalog, String schema, String table)           
    Retrieves a description of the given table's primary key columns
    得到主键2。getImportedKeys(String catalog, String schema, String table)           
    Retrieves a description of the primary key columns that are referenced by a table's foreign key columns (the primary keys imported by a table)
    得到和表主键相关的所有外键3。getCrossReference(String primaryCatalog, String primarySchema, String primaryTable, String foreignCatalog, String foreignSchema, String foreignTable)          
     Retrieves a description of the foreign key columns in the given foreign key table that reference the primary key columns of the given primary key table (describe how one table imports another's key).