java - Match Table Name and Alias at runtime from DatabaseMetaData? -


i know how use .gettables() table , alias entries.

final resultset rs = dmd.gettables(null, "myschema", "%", new string[]{"table","alias"}); 

what need able determine alias goes table name?

i need able on oracle, sql server , db2.

and given have table t00001 , has alias of mytable how can match 2 using databasemetadata connection.getmetadata() call?

after reviewing jdbc apis , oracle.jdbc.oracledatabasemetadata, i'm pretty there no way retrieve information directly through databasemetadata when using stock oracle jdbc drivers. there may driver-specific ways db2 or sql server didn't check them out thoroughly. cheat, can getconnection() databasemetadata object , run database-specific queries retrieve information need, follows:

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);             // use conn.getmetadata().getconnection() instead of conn here fit within parameters of question             preparedstatement stmt = conn.getmetadata().getconnection().preparestatement(sql);             resultset rs = stmt.executequery();         ) {             // may not want if 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 table aliases %s:\n", dbname);         (map.entry<string, list<string>> entry : tablealiases.entryset()) {             system.out.format("the alias(es) %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 user_synonyms")); // or maybe all_synonyms          mapper.print("db2",             mapper.maptablealiases(                 "jdbc:db2://localhost:50000/sample",                 "db2admin",                 "db2admin",                 "select base_tabname, tabname syscat.tables type = 'a' , owner = 'db2admin'"));          mapper.print("sql server",             mapper.maptablealiases(                 "jdbc:sqlserver://localhost:1433",                 "sa",                 "password123",                 "select parsename(base_object_name,1), name sys.synonyms"));     } } 

code tested using jdk 1.8.0_45, oracle xe 11.2.0.2.0 , bundled jdbc driver, db2 express-c 10050500 , bundled jdbc driver, , sql server 2014 express 12.0.2000.8 microsoft jdbc driver 4.1.5605.100.

sql server query based on http://sqlblog.com/blogs/john_paul_cook/archive/2010/08/24/script-to-list-synonym-contents.aspx.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -