//
//   Source File Name: Tools.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. 
//  
//   This is a toolkit for DB2 Java program samples. 
//  

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

class Tools {

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

   //  
   //  DBConnect 
   //    - if provided with database name, user ID, and password,  
   //      connects to database 
   //    - otherwise prompts user for database name, user ID, and 
   //      password and connects to database  
   //    - returns the connection 
   //  

   public static Connection DBConnect(String dbname, String uid, String pwd)
   {
      Connection con = null;
      try {
         String url;
         //  URL is jdbc:db2:dbname 
         url = "jdbc:db2:" + dbname;
         //  connect with id and password 
         con = DriverManager.getConnection(url, uid, pwd);
         System.out.println (">Connected to " + dbname);
      } catch (Exception e) { e.printStackTrace(); }
      return con;
   }

   //  prompt for database, user id, and password 
   public static Connection DBConnect()
   {
      Connection con = null;
      try {
         String dbname, url, uid, pwd;
         System.out.println (">Enter Database Name:");
         dbname = readString();
         System.out.println (">Enter User Name:");
         uid = readString();
         System.out.println (">Enter Password:");
         pwd = readString();

         //  URL is jdbc:db2:dbname 
         url = "jdbc:db2:" + dbname;
         //  connect with id and password 
         con = DriverManager.getConnection(url, uid, pwd);
      } catch (Exception e) { e.printStackTrace(); }
      return con;
   }

   //  
   //  readString 
   //    - declares the input stream to be the system.in and reads in 
   //    - a string - returns this such string 
   //  

   public static String readString() {
      try {
         InputStreamReader isr = new InputStreamReader(System.in);
         BufferedReader br = new BufferedReader(isr);
         String r = br.readLine();
         return r;
      }
      catch( IOException e) { return ""; }
   }


   //  
   //  padLength 
   //    - adds spaces to input on the right to make n chars long 
   //    = keeps the string on the left of the output string 
   //  

   public static String padLength(String input, int n) {
      try {
         while (input.length() < n) { input = input + " "; }
         return input;
      }
      catch( Exception e) { return ""; }
   }

   //  
   //  padLengthRight 
   //    - adds spaces to input on the left to make n chars long 
   //    - the new string has the old string on the utmost right 
   //  

   public static String padLengthRight(String input, int n) {
      try {
         while (input.length() < n) { input = " " + input; }
         return input;
      }
      catch( Exception e) { return ""; }
   }
}