//
//   Source File Name: Procs.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 getProcedures 
//   - prompts for pattern search values for procedure schema name 
//   - lists all procedures under search values 

//  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.*;

class Procs {

   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);

         System.out.println ("Enter Procedure Schema Name Search Pattern:");
         String procSchemSearchPat = Tools.readString().toUpperCase();
         listProcedures (con, procSchemSearchPat, "%");

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

   //  
   //  listProcedures 
   //    - lists all procedures that match user defined search patterns 
   //  

   public static void listProcedures (Connection con, String procSchemSearchPat, String procTableSearchPat) {
      try {
         DatabaseMetaData dmd = con.getMetaData();
         System.out.println ("PROCEDURE SCHEMA\tPROCEDURE NAME");
         System.out.println ("----------------------- -----------------------");
         String procSchema = new String("");
         String procName = new String("");
         String remarks = new String("");

         //  output procedure shemas and names that fit search pattern 
         ResultSet rs = dmd.getProcedures (null, procSchemSearchPat, "%");
         while (rs.next()) {
            procSchema = rs.getString (2);
            procName = rs.getString (3);
            remarks = rs.getString (7);
            System.out.println (Tools.padLength(procSchema, 24) + procName);
            if (!rs.wasNull()) {
               System.out.println ("  (Remarks) " + remarks);
            }
         }
         rs.close();
      } catch (Exception e) { e.printStackTrace(); }
   }
}