//
//   Source File Name: Tables.java  1.3 
//  
//   Licensed Materials -- Property of IBM 
//  
//   (c) Copyright International Business Machines Corporation, 1999. 
//       All Rights Reserved. 
//  
//   US Government Users Restricted Rights - 
//   Use, duplication or disclosure restricted by 
//   GSA ADP Schedule Contract with IBM Corp. 
//  
//     PURPOSE : 
//   Example using of using getTables 
//   - prompts for pattern search values for schema name, table name 
//   - lists all tables that match search values and indicates table types 

//  For more information about this sample, refer to the README file. 

//  For more information on Programming in Java, refer to the  
//  "Programming in Java" section of the Application Development Guide. 

//  For more information on building and running Java programs for DB2, 
//  refer to the "Building Java Applets and Applications" section of the  
//  Application Building Guide. 

//  For more information on the SQL language, refer to the SQL Reference. 

import java.io.*;
import java.lang.*;
import java.sql.*;
import java.util.*;

class Tables {

   static {
      try {
         //  register the driver with DriverManager 
         //  The newInstance() call is needed for the sample to work with 
         //  JDK 1.1.1 on OS/2, where the Class.forName() method does not 
         //  run the static initializer. For other JDKs, the newInstance 
         //  call can be omitted. 
         Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void main (String[] argv) {
      Connection con = null;

      try {
         //  see Tools.class on Tools.DBConnect method 
         if (argv.length == 3)
         {  //  connect using command line arguments 
            con = Tools.DBConnect(argv[0], argv[1], argv[2]); 
         }
         else
         {  //  prompt user for database name, user ID, password 
            con = Tools.DBConnect(); 
         }
         con.setAutoCommit(false);
         DatabaseMetaData dmd = con.getMetaData();

         //  allow for upper or lower case entries 
         System.out.println ("Enter Table Schema Name Search Pattern:");
         String tableSchemSearchPat = Tools.readString().toUpperCase();
         System.out.println ("Enter Table Name Search Pattern:");
         String tableSearchPat = Tools.readString().toUpperCase();
         //  place all types from vector into an array 
         Vector tTypes = getTypes (con);
         String [] arrayOfTypes = new String[tTypes.size()];
         tTypes.copyInto(arrayOfTypes);
         displayTables (con, tableSchemSearchPat, tableSearchPat, arrayOfTypes);

         //  disconnect from server 
         System.out.print ("\n>Disconnecting...");
         con.commit();
         con.close();
      } catch (Exception e) { e.printStackTrace(); }
   }

   //  
   //  getTypes 
   //     - stores all types supported by datase in a Vector 
   //  

   public static Vector getTypes (Connection con) {
      Vector tableTypes = new Vector();
      try {
         DatabaseMetaData dmd = con.getMetaData();
         ResultSet rs = dmd.getTableTypes();
         String currentType = new String("");
         while (rs.next())  {
            //  add each new type to the Vector 
            currentType = rs.getString (1);
            tableTypes.addElement (currentType);
         }
         rs.close();
      } catch (Exception e) { e.printStackTrace(); }
      return tableTypes;
   }

   //  
   //  displayTables 
   //     - lists tables that matches search patterns 
   //  

   public static void displayTables (Connection con, String schemSearchPat, String tableSearchPat, String[] tableTypes) {
      try {
         DatabaseMetaData dmd = con.getMetaData();
         ResultSet rs = dmd.getTables (null, schemSearchPat, tableSearchPat, tableTypes);
         System.out.println ("TABLE SCHEMA\t\tTABLE_NAME\t\tTABLE_TYPE");
         System.out.println ("----------------------- ----------------------- ----------");

         String tableSchema = new String("");
         String table = new String("");
         String tType = new String("");

         //  output tables and types that fit search pattern 
         while (rs.next()) {
            //  format and read in text for output 
            tableSchema = rs.getString (2);
            table = rs.getString (3);
            tType = rs.getString (4);
            System.out.println (Tools.padLength(tableSchema, 24) + Tools.padLength(table, 24) + tType);
         }
         rs.close();
      } catch (Exception e) { e.printStackTrace(); }
   }
}