是否可以使用JDBC获取FK的目标?

我只使用getExportedKeys方法获得了Source Relation + Attributes:

ResultSet fksE = dbm.getExportedKeys(conn.getCatalog(), null, "SourceRelName");
String fkTableNameE = fksE.getString("FKTABLE_NAME");
String fkColumnNameE = fksE.getString("FKCOLUMN_NAME");

然后,我尝试使用getImportedKeys方法,但没有成功。

这是我的课:
public class DbConnection {

  private String userName = null;
  private String password = null;
  private String driver = null;
  private String DbUrl = null;
  private Connection conn = null;
  private DatabaseMetaData dbm = null;
  private ArrayList<String> relationNames = null;
  private ResultSet tables = null;
  private Database database = null;

  public DbConnection(String user, String pwd, dbaCore.data.dBTypes.TypeEnum type, String url) {
    userName = user;
    password = pwd;
    switch (type) {
      case MYSQL:
        driver = "com.mysql.jdbc.Driver";
        break;
      case POSTGRES:
        break;
      case MSDB:
        break;
      case ORACLE:
        break;
      case SQLITE:
        break;
    }
    DbUrl = url;
    database = new Database();
    relationNames = new ArrayList<>();

    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(DbUrl, userName, password);
      System.out.println("Database connection established");

      //Get Relation Names
      dbm = conn.getMetaData();
      String[] types = {"TABLE"};
      tables = dbm.getTables(null, null, "%", types);
      while (tables.next()) {
        String table = tables.getString("TABLE_NAME");
        relationNames.add(table);
      }

      //Get Attributes
      for (String relation : relationNames) {
        RelationSchema tmpRelation = new RelationSchema(relation);

        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM " + relation);
        ResultSetMetaData md = rs.getMetaData();
        int col = md.getColumnCount();
        System.out.println("ATTRIBUTES: ");
        for (int i = 1; i <= col; i++) {
          String col_name = md.getColumnName(i);
          System.out.println(col_name);
          tmpRelation.addAttribute(col_name);
        }
        ResultSet pks = dbm.getPrimaryKeys(null, null, relation);
        while (pks.next()) {
          String columnName = pks.getString("COLUMN_NAME");
          System.out.println("Primary Key: " + columnName);
          tmpRelation.getAttributeByName(columnName).setIsPrimaryKey(true);
        }
        database.addRelationSchema(tmpRelation);
      }

      //Get Foreignkeys
      for (String relation : relationNames) {
        ResultSet fksE = dbm.getExportedKeys(conn.getCatalog(), null, relation);

        while (fksE.next()) {
          String fkTableNameE = fksE.getString("FKTABLE_NAME");
          String fkColumnNameE = fksE.getString("FKCOLUMN_NAME");
      ResultSet fksI = dbm.getImportedKeys(conn.getCatalog(), null, fkTableNameE);
      String fkTableNameI = fksI.getString("FKTABLE_NAME");
      String fkColumnNameI = fksI.getString("FKCOLUMN_NAME");

      System.out.println("FKTABLE_NAME_E: " + fkTableNameE + " - FKCOLUMN_NAME_E: " + fkColumnNameE);
      System.out.println("FKTABLE_NAME_I: " + fkTableNameI + " - FKCOLUMN_NAME_I: " + fkColumnNameI);
    }
  }

} catch (Exception e) {
  System.err.println("Cannot connect to database server");
  e.printStackTrace();
}
finally
{
  if (conn != null) {
    try {
      conn.close();
      System.out.println("Database connection closed");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


}

最佳答案

getImportedKeys为我工作。一些代码:

private static void printForeignKeys(Connection connection, String tableName) throws SQLException {
    DatabaseMetaData metaData = connection.getMetaData();
    ResultSet foreignKeys = metaData.getImportedKeys(connection.getCatalog(), null, tableName);
    while (foreignKeys.next()) {
        String fkTableName = foreignKeys.getString("FKTABLE_NAME");
        String fkColumnName = foreignKeys.getString("FKCOLUMN_NAME");
        String pkTableName = foreignKeys.getString("PKTABLE_NAME");
        String pkColumnName = foreignKeys.getString("PKCOLUMN_NAME");
        System.out.println(fkTableName + "." + fkColumnName + " -> " + pkTableName + "." + pkColumnName);
    }
}

关于java - JDBC获取外键引用的关系+属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11669341/

10-10 03:40