//***************************************************************************
// (c) Copyright IBM Corp. 2007 All rights reserved.
// 
// The following sample of source code ("Sample") is owned by International 
// Business Machines Corporation or one of its subsidiaries ("IBM") and is 
// copyrighted and licensed, not sold. You may use, copy, modify, and 
// distribute the Sample in any form without payment to IBM, for the purpose of 
// assisting you in the development of your applications.
// 
// The Sample code is provided to you on an "AS IS" basis, without warranty of 
// any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR 
// IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do 
// not allow for the exclusion or limitation of implied warranties, so the above 
// limitations or exclusions may not apply to you. IBM shall not be liable for 
// any damages you suffer as a result of using, copying, modifying or 
// distributing the Sample, even if IBM has been advised of the possibility of 
// such damages.
//***************************************************************************
//
// SOURCE FILE NAME: GetDBMCfgParams.java
//
// SAMPLE: Use the view SYSIBMADM.DBMCFG to retrieve a database 
//         manager configuration parameter.
//
// Run this sample using the following steps:
//         1. Create and populate the "sample" database 
//            with the following command:
//            db2sampl
//
//         2. Compile the program with the following command:
//            javac GetDBMCfgParams.java Util.java
//
//         3. Run this sample with the following command:
//            java GetDBMCfgParams <configuration parameter names>
//
// JAVA 2 CLASSES USED:
//         Statement
//         ResultSet
//
// Class used from Util.java are:
//         JdbcException
//
//                           
// Output will vary depending on the JDBC driver connectivity used.
//***************************************************************************
//
// For more information on the sample programs, see the README file.
//
// For information on developing Java applications see the Developing Java Applications book.
//
// For information on using SQL statements, see the SQL Reference.
//
// For the latest information on programming, compiling, and running DB2
// applications, visit the DB2 Information Center at
//     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
//**************************************************************************/

import java.lang.*;
import java.sql.*;

class GetDBMCfgParams
{
  public static void main(String argv[])
  {    
  
    System.out.print("--------------------------------------------------");
    System.out.println("----------------------------------------");         
    System.out.print("THIS SAMPLE SHOWS HOW TO CALL THE UDF DBM_GET_CFG ");
    System.out.println("AND RETRIEVE DBM CONFIGURATION PARAMETERS.");
    System.out.print("--------------------------------------------------");    
    System.out.println("----------------------------------------");        
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;

    if (argv.length < 1)
    {
      System.out.print("Missing input arguments. Enter one or");
      System.out.println(" more configuration parameter names.");
    }
    else
    {           
      try
      {
        // initialize DB2Driver and establish database connection.
        Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
        con = DriverManager.getConnection("jdbc:db2:SAMPLE");

        // create the SQL statement and execute.
        stmt = con.createStatement();
  
        String whereClause = "WHERE NAME IN (";
        
        for (int i = 0; i < argv.length; i++)
        {
          whereClause += "'" + argv[i].trim() + "',";
        }
        
        whereClause = whereClause.substring(0, whereClause.length()-1) + ")";
 
        String stmtText = "SELECT NAME, VALUE, DEFERRED_VALUE, DATATYPE " +
                          "FROM SYSIBMADM.DBMCFG " + 
                          whereClause;
                          
        System.out.println(stmtText);

        rs = stmt.executeQuery(stmtText);

        while (rs.next()) 
        {        
          String paramName = rs.getString("NAME").trim();
          String paramValue = rs.getString("VALUE");
          String paramDeferredValue = rs.getString("DEFERRED_VALUE");
          String paramType = rs.getString("DATATYPE").trim();
        
          paramValue = (paramValue == null) ? "" : paramValue.trim();
          paramDeferredValue = (paramDeferredValue == null) ? 
                                 "" : paramDeferredValue.trim();
          System.out.println();
          System.out.println("Parameter Name           = " + paramName);
          System.out.println("Parameter Value          = " + paramValue);
          System.out.print("Parameter Deferred Value = ");
          System.out.println(paramDeferredValue);
          System.out.println("Parameter Data Type      = " + paramType);
                        
          // cast parameter value to appropriate type if needed.
          if (paramType.equals("INTEGER")) 
          {
            int value = Integer.parseInt(paramValue);
          }
          else if (paramType.equals("BIGINT")) 
          {
            long value = Long.parseLong(paramValue);
          }
          else if (paramType.equals("DOUBLE"))
          {
            double value = Double.parseDouble(paramValue);
          }
          else if (paramType.startsWith("VARCHAR"))
          {
            String value = paramValue;
          }
        }
      }
      catch (Exception e)
      {
       JdbcException jdbcExc = new JdbcException(e);
       jdbcExc.handle();
      }      
      finally
      {
        try
        {
          // close the resultset
          rs.close();      

          // close the statement
          stmt.close();
    
          // close the connection
          con.close();   
        }
        catch (Exception x)
        { 
          System.out.print("\n Unable to Rollback/Disconnect ");
          System.out.println("from 'sample' database"); 
        }
      }
    }
  } // main
} // GetDBMCfgParams