3

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

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

我需要能够确定的是哪个ALIAS与哪个TABLE名字搭配?

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

鉴于我有一张桌子T00001,它有一个别名,MyTable我如何使用DatabaseMetaDatafromConnection.getMetaData()调用将两者匹配?

4

1 回答 1

1

在查看了 JDBC API 和oracle.jdbc.OracleDatabaseMetaData之后,我很确定在使用库存的 Oracle JDBC 驱动程序时无法直接通过DatabaseMetaData检索这些信息。DB2 或 SQL Server 可能有特定于驱动程序的方法,因为我没有彻底检查它们。作为一个骗子,您可以getConnection()从 DatabaseMetaData 对象并运行特定于数据库的查询来检索您需要的信息,如下所示:

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 驱动程序以及带有 Microsoft JDBC 驱动程序 4.1.5605.100 的 SQL Server 2014 Express 12.0.2000.8 成功测试了代码。

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

于 2015-07-12T23:36:06.750 回答