在运行时从 DatabaseMetaData 匹配 Table 名称和别名?

Match Table Name and Alias at runtime from DatabaseMetaData?

我知道如何使用 .getTables() 获取 TABLEALIAS 条目。

final ResultSet rs = dmd.getTables(null, "MySchema", "%", new String[]{"TABLE","ALIAS"});

我需要确定的是哪个 ALIAS 与哪个 TABLE 名称匹配?

我特别需要能够在 OracleSQL ServerDB2 上执行此操作。

鉴于我有一个 table T00001 并且它有一个 MyTable 的别名,我如何使用 [=23] 中的 DatabaseMetaData 匹配两者=]打电话?

在查看 JDBC API 和 oracle.jdbc.OracleDatabaseMetaData, I'm pretty certain that there is no way to retrieve this information directly through DatabaseMetaData 使用原装 Oracle JDBC 驱动程序后。 DB2 或 SQL 服务器可能有特定于驱动程序的方法,因为我没有彻底检查它们。作为作弊,您可以从 DatabaseMetaData 对象 getConnection() 和 运行 特定于数据库的查询中检索您需要的信息,如下所示:

public class TableAliasMapperJdbc {
    public Map<String, List<String>> mapTableAliases(String url, String user, String password, String sql) throws SQLException {
        try (
            Connection conn = DriverManager.getConnection(url, user, password);
            // we use conn.getMetaData().getConnection() instead of conn here only to fit within the parameters of the question
            PreparedStatement stmt = conn.getMetaData().getConnection().prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
        ) {
            // this may not do what you want if you have synonyms of synonyms
            Map<String, List<String>> tableAliases = new HashMap<>();
            while (rs.next()) {
                String table = rs.getString(1);
                String alias = rs.getString(2);
                List<String> aliases = tableAliases.get(table);
                if (aliases == null) {
                    tableAliases.put(table, aliases = new ArrayList<>(2));
                }
                aliases.add(alias);
            }
            return tableAliases;
        }
    }

    public void print(String dbName, Map<String, List<String>> tableAliases) {
        System.out.format("\nThe following are the table aliases for %s:\n", dbName);
        for (Map.Entry<String, List<String>> entry : tableAliases.entrySet()) {
            System.out.format("The alias(es) for %s are: %s.\n", entry.getKey(), String.join(", ", entry.getValue()));
        }
    }

    public static void main(String[] args) throws SQLException {
        TableAliasMapperJdbc mapper = new TableAliasMapperJdbc();
        mapper.print("Oracle",
            mapper.mapTableAliases(
                "jdbc:oracle:thin:@localhost:1521:xe",
                "scott",
                "tiger",
                "SELECT table_name, synonym_name FROM user_synonyms")); // or maybe all_synonyms

        mapper.print("DB2",
            mapper.mapTableAliases(
                "jdbc:db2://localhost:50000/SAMPLE",
                "db2admin",
                "db2admin",
                "SELECT base_tabname, tabname FROM syscat.tables WHERE type = 'A' AND owner = 'DB2ADMIN'"));

        mapper.print("SQL Server",
            mapper.mapTableAliases(
                "jdbc:sqlserver://localhost:1433",
                "sa",
                "Password123",
                "SELECT PARSENAME(base_object_name,1), name FROM sys.synonyms"));
    }
}

代码已使用 JDK 1.8.0_45、Oracle XE 11.2.0.2.0 及其捆绑的 JDBC 驱动程序、DB2 Express-C 10050500 及其捆绑的 JDBC 驱动程序,以及 SQL Server 2014 Express 12.0.2000.8 with Microsoft JDBC Driver 4.1.5605.100.

SQL 服务器查询基于 http://sqlblog.com/blogs/john_paul_cook/archive/2010/08/24/script-to-list-synonym-contents.aspx