//
//   Source File Name: MultiCon.java  1.2 
//  
//   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. 
//  
//   This sample program shows how to connect and disconnect properly to 
//   more than one connection - unlike multicon.c error checking is performed 
//   behind the scenes with JDBC 

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

   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) {
      int MAX_CONNECTIONS = 5;
      Connection con[] = new Connection[MAX_CONNECTIONS];  //  will handle handles with an array 
      int cont;

      try {
         String ans = new String("");
         int cnt = 0;

         while (!(ans.startsWith("n")) && (cnt != MAX_CONNECTIONS)) {
            System.out.println ("Connect to another Data Source?(Y or N)");
            ans = Tools.readString().toLowerCase();
            if (ans.startsWith("y")) {
               //  connect with default id/password 
               con[cnt] = Tools.DBConnect();
               cnt = cnt + 1;  //  increase connection indicator 
            }
         }

         // disconnect from all connections made 
         System.out.println ("Disconnecting...");
         int clscnt;
         for (clscnt = 0; clscnt < cnt; clscnt = clscnt + 1) {
            con[clscnt].close();
         }

      } catch (Exception e) { e.printStackTrace(); }
   }
}