//
//  Source File Name: GetAttrs.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 : 
//     - Retrieves and displays environment, connection and statement 
//     attributes (options). 

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

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

         getAttributes (con);

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

   //  
   //  getAttributes 
   //  

   public static void getAttributes (Connection con) {
      try {
         DatabaseMetaData dmd = con.getMetaData();
         System.out.println ("--------------------------------------------") ;

         String url = dmd.getURL();
         System.out.println ("   URL to Server: " + url);
         System.out.println ("   Instance Name: (NOT APPLICABLE)");

         String dbname = dmd.getDatabaseProductName();
         System.out.println ("       DBMS Name: " + dbname);

         String dbversion = dmd.getDatabaseProductVersion();
         System.out.println ("    DBMS Version: " + dbversion);
         System.out.println ("--------------------------------------------") ;

         boolean autoCommit = con.getAutoCommit();
         System.out.print ("Autocommit is: ");
         if (autoCommit) { System.out.print ("ON\n"); } else { System.out.print ("OFF\n"); }

         boolean cursorWithHold = dmd.supportsOpenCursorsAcrossCommit();
         System.out.print ("Cursor With Hold is: ");
         if (cursorWithHold) { System.out.print ("ON\n"); } else { System.out.print ("OFF\n"); }
      } catch (Exception e) { e.printStackTrace(); }
   }
}