Traverse schema model

This example demonstrates traversing the schema model of a JDBC database connection:

   1 Connection con = ...
   2 
   3 DataContext dataContext = DataContextFactory.createJdbcDataContext(con);
   4 Schema[] schemas = dataContext.getSchemas();
   5 
   6 // iterate through schemas
   7 for (Schema schema : schemas) {
   8   
   9   System.out.println(schema.getName());
  10   Table[] tables = schema.getTables();
  11   
  12   // iterate through tables
  13   for (Table table : tables) {
  14     
  15     System.out.println("  " + table.getName() + " (" + table.getType() + ")");
  16     Column[] columns = table.getColumns();
  17     
  18     // iterate through columns
  19     for (Column column : columns) {
  20     
  21         System.out.println("    " + column.getName() + " (" + column.getType() + "|" + column.getNativeType() + ")");
  22     }
  23   } 
  24 }

Run on an example PostgreSQL database this will print out:

information_schema
pg_catalog
pg_toast_temp_1
public
  person (TABLE)
    id (INTEGER|serial)
    name (VARCHAR|text)
    age (INTEGER|integer)
    company_id (INTEGER|int4)
  company (TABLE)
    id (INTEGER|serial)
    name (VARCHAR|text)
  employments (VIEW)
    employee_name (VARCHAR|text)
    company_name (VARCHAR|text)


CategoryExamples

examples/TraverseSchemaModel (last edited 2013-08-01 09:11:09 by KasperSorensen)