//
//   Source File Name: ProcCols.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 getProcedureColumns 
//     - prompts for pattern search values for schema name, table name 

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

   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();
         System.out.println ("Enter Procedure Name Search Pattern:");
         String procNameSearchPat = Tools.readString().toUpperCase();

         listProcedureColumns (con, procSchemSearchPat, procNameSearchPat, "%");

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

   //  
   //  listProcedureColumns 
   //     - lists procedure columns that match search patterns passed into method 
   //  

   public static void listProcedureColumns (Connection con, String procSchemSearchPat, String procNameSearchPat, String colNamePat) {
      try {
         DatabaseMetaData dmd = con.getMetaData();
         ResultSet rs = dmd.getProcedureColumns (null, procSchemSearchPat, procNameSearchPat, colNamePat);

         String procSchema = new String("");
         String procName = new String("");
         String schemaOld = new String("");
         String nameOld = new String("");
         String colName = new String("");
         String argType = new String("");
         String typeName = new String("");
         String scaleOutput = new String("");
         String remarks = new String("");
         short scale = 0;
         int length = 0;

         //  print procedure shemas and names that fit search pattern 
         while (rs.next()) {
            //  retrieve necessary data from result set query and format for output 
            procSchema = rs.getString (2);
            procName = rs.getString (3);
            if (!procSchema.equalsIgnoreCase (schemaOld) || !procName.equalsIgnoreCase (nameOld)) {
               System.out.println ("\n" + procSchema + "." + procName);
               schemaOld = procSchema;
               nameOld = procName;
            }
            colName = rs.getString (4);
            if (rs.getShort (5) == 3) { argType = ", Input" ; }
            if (rs.getShort (5) == 4) { argType = ", Output" ; }
            if (rs.getShort (5) == 5) { argType = ", Input_Output" ; }
            typeName = ", " + rs.getString (7);
            length = rs.getInt (8);
            scale = rs.getShort (10);
            if (!rs.wasNull()) { scaleOutput = ", " + String.valueOf (scale) + ")"; }
            else { scaleOutput = ")"; }
            remarks = rs.getString (13);
            if (rs.wasNull()) { remarks = ""; } else { remarks = ", " + remarks; }

            System.out.println ("   " + colName + argType + typeName + ", (" + length + scaleOutput + remarks);
         }
         rs.close();
      } catch (Exception e) { e.printStackTrace(); }
   }
}