简体   繁体   中英

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. The database does contain foreign keys relations and getPrimaryKey() does work. 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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