//
//   Source File Name: DropJava.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 : 
//   Drops all Tables, User Defined Types & Functions for the Order scenario. 

//  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 DropJava {

   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 {
         //  connect to the server of user's choice 
         //  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);

         Vector allStatements = createStatements();
         String[] statements = new String[allStatements.size()];
         allStatements.copyInto (statements);
         executeStatements (con, statements, allStatements.size());

         //  disconnect from the connection made 
         System.out.println (">Disconnecting...");
         con.commit();
         con.close();
      } catch (Exception e) { e.printStackTrace(); }
   }

   //  
   //  createStatements 
   //     - adds a number of SQL statements to a vector 
   //  

   public static Vector createStatements() {
      Vector statements = new Vector();
      try {
        statements.addElement ("DROP FUNCTION PRICE( CHAR(12), PUNIT, char(16) )");
        statements.addElement ("DROP FUNCTION \"+\"( PNUM, INTEGER )");
        statements.addElement ("DROP FUNCTION MAX( PNUM )");
        statements.addElement ("DROP TABLE CUSTOMER");
        statements.addElement ("DROP TABLE PRODUCT");
        statements.addElement ("DROP TABLE PROD_PARTS");
        statements.addElement ("DROP TABLE ORD_CUST");
        statements.addElement ("DROP TABLE ORD_LINE");
        statements.addElement ("DROP DISTINCT TYPE CNUM");
        statements.addElement ("DROP DISTINCT TYPE PUNIT");
        statements.addElement ("DROP DISTINCT TYPE UPRICE");
        statements.addElement ("DROP DISTINCT TYPE PRICE");
        statements.addElement ("DROP DISTINCT TYPE PNUM");
        statements.addElement ("DROP DISTINCT TYPE ONUM");
      } catch (Exception e) { e.printStackTrace(); }
      return statements;
   }

   //  
   //  executeStatements 
   //     - executes all statements found in the array of statements 
   //  

   public static void executeStatements (Connection con, String[] statement, int total) {
      try {
         for (int cnt = 1; cnt <= total; cnt = cnt + 1) {
            Statement stmt = con.createStatement();
            System.out.println (">Executing Statement " + cnt + ":");
            System.out.println ("    " + statement[cnt-1]);
            //  executes the current statement 
            stmt.executeUpdate (statement[cnt-1]);
            stmt.close();
         }
         con.commit();
      } catch (Exception e) { e.printStackTrace(); }
   }
}