0

Is there a JDBC method to get child tables and all of its descendants.

getExportedKeys returns only the direct child and not all its descendants. If I keep calling getExportedKeys() method recursively , it takes around 3 minutes for 50 tables or so (which is really slow)

Could someone please help me out for a solution.

Main objective is to avoid using queries since I would be dealing with different databases.

Recursive function used:

private static void getChildTables(DatabaseMetaData dbmd,Set<String> dependencies,String tableName,String schemaname ) throws SQLException{
       ResultSet rs3 = dbmd.getExportedKeys(null,schemaname, tableName);    
         while (rs3.next()){       
if(dependencies.contains(rs3.getString(7)))
   continue;
 dependencies.add(rs3.getString(7));
 String childTable=rs3.getString(7);
getChildTables(dbmd, dependencies, childTable, null);
 rs3.close();
 rs3 = dbmd.getExportedKeys(null,schemaname, tableName);                    
         }
Daniel M
  • 3
  • 2
  • What do you mean with "child tables", do you mean tables with a foreign key reference to a specific table? In any case: no there is no method in JDBC to do this. – Mark Rotteveel Jul 25 '15 at 07:16
  • Related: http://stackoverflow.com/questions/28207861/get-table-dependency-order-in-jdbc – Mark Rotteveel Jul 25 '15 at 07:44
  • Thanks @Mark for the link. Yes Child tables is the one with a foreign key reference to a specific table. It is the morever the same problem as specified in the link. Example: if A->B A has B as child and Say B->C B has C as child . getExportedkeys called on A would return only B and not C. I need a way to get child tables and and all its descendants. Is there any other alternate way(work-around) of getting it so that it will work for all databases. – Daniel M Jul 25 '15 at 16:11

0 Answers0