public static Map < String , ForeignKey > getExportedKeys ( DatabaseMetaData metadata , String tableName ) throws Exception { ResultSet keys = metadata . getExportedKeys ( metadata . getConnection ( ) . getCatalog ( ) , metadata . getUserName ( ) , tableName ) ; Map < String , ForeignKey > map = new HashMap < String , ForeignKey > ( ) ; while ( keys . next ( ) ) { String table = keys . getString ( EXPORTED_FK_TAB_NAME ) ; String name = keys . getString ( EXPORTED_FK_KEY_NAME ) ; if ( name == null || name . length ( ) == 0 ) name = "UNNAMED_FK_" + table ; ForeignKey key = map . get ( name ) ; if ( key == null ) { map . put ( name , key = new ForeignKey ( table ) ) ; } key . add ( keys . getString ( EXPORTED_FK_COL_NAME ) ) ; } keys . close ( ) ; return map ; }
Returns a table s exported keys and their columns as a Map from the key name to the ForeignKey object .