简体   繁体   English

无法使用getImportedKeys()或getExportedKeys()获取外键,仅getPrimaryKeys()有效

[英]Unable to get foreign keys using getImportedKeys() or getExportedKeys(), only getPrimaryKeys() work

I am not able to find foreign keys using either getImportedKeys() or getExportedKeys() from the metadata. 我无法从元数据中使用getImportedKeys()或getExportedKeys()找到外键。 The database does contain foreign keys relations and getPrimaryKey() does work. 该数据库确实包含外键关系,并且getPrimaryKey()起作用。 How could I fix this? 我该如何解决?

DatabaseMetaData dbMeta = conn.getMetaData();
System.out.println("Foreign Keys are\n");
ResultSet rs = dbMeta.getExportedKeys("", "", "CUSTOMERS");
while (rs.next()) {
    //System.out.println(rs.getString("FKCOLUMN_NAME"));
    System.out.println(rs.getString("FK_NAME") + "\t" + rs.getString("FKCOLUMN_NAME"));
    }

try this out and see if it works :) 试试看,看看是否可行:)

private static void printForeignKeys(Connection connection, String CUSTOMERS) throws SQLException {
    System.out.println("Foreign Keys are\n");
    DatabaseMetaData dbMeta= connection.getMetaData();
    ResultSet foreignKeys = dbMeta.getImportedKeys(connection.getCatalog(), null, CUSTOMERS);
    while (foreignKeys.next()) {
        String fkName = foreignKeys.getString("FK_NAME");
        String fkColumnName = foreignKeys.getString("FKCOLUMN_NAME");
        System.out.println(fkName + "." + fkColumnName);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM