
// ViewTables.java
// Andrew Davison, October 22nd 2008, ad@fivedots.coe.psu.ac.th

// Illustrates the use of meta data from a database
// Uses getTables()

import java.sql.*;


public class ViewTables {

  public static void main(String[] args)
  {
    // The URL for the Books database.
    // It is 'protected' by a login and password.
    String url = "jdbc:odbc:Books";  
    String username = "anonymous";
    String password = "guest";

    try {
      // load the JDBC-ODBC Bridge driver
      Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

      // connect to the database using the DriverManager
      Connection conn = DriverManager.getConnection( url, 
					                        username, password );

      // Get the database metadata
  	  DatabaseMetaData metadata = conn.getMetaData();
       
      // Extract the table-based metadata
      String [] tableTypes = { "TABLE" };
      ResultSet tables = 
              metadata.getTables(null, null, null, tableTypes );

      // Print the table names
      String tableName;
      while( tables.next() ) {
        tableName = tables.getString("TABLE_NAME");
        System.out.println( tableName );
      }
      conn.close();
    } 
    catch ( ClassNotFoundException cnfex ) {
      System.err.println("Failed to load JDBC/ODBC driver." );
      cnfex.printStackTrace();
      System.exit( 1 );  // terminate program
    }
    catch ( SQLException sqlex ) {
      System.err.println( sqlex );
      sqlex.printStackTrace();
    }
  } // end of main()

} // end of ViewTables class


