/**
  * 从apache提供的连接池中取连接,失败返回false
  *
  * @return boolean
  * @date:
  */
 private boolean getApachecommonDBCP() {
   try {
     Context tContext = new InitialContext();
     tContext = (Context) tContext.lookup("java:comp/env");
     Object obj = tContext.lookup(JUrl.getDBName());
     DataSource tDataSource = (DataSource) obj;
     if (tDataSource != null) {
       con = tDataSource.getConnection();
       // 如果连接的是Oracle数据库,需要稍微处理一下日期的格式,最好是在服务器哪里设置一下,而不调用下面的程序
       // 可以添加一个字段类型,来控制是否使用下面的语句
       if (con != null) {
         //                        Statement stmt = con.createStatement(ResultSet.
         //                                TYPE_SCROLL_SENSITIVE,
         //                                ResultSet.CONCUR_UPDATABLE);
         //                        stmt.execute(
         //                                "alter session set nls_date_format = 'YYYY-MM-DD
         // HH24:MI:SS'");
         //                        stmt.close();
         return true;
       }
       return false;
     } else {
       System.out.println("a error occured when geting datasource");
       return false;
     }
   } catch (Throwable e) {
     System.out.println("failure when connect apache commons dbcp ");
     e.printStackTrace();
     return false;
   }
 }
  /**
   * 对于Weblogic连接池
   *
   * @return boolean
   */
  private boolean getWeblogicPoolConnection() {
    try {
      Driver myDriver = (Driver) (Class.forName("weblogic.jdbc.pool.Driver").newInstance());
      /*weblogic的连接池重写了close()方法*/
      con = myDriver.connect(JUrl.getJdbcUrl(), null);
      Statement stmt =
          con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      stmt.execute("alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'");
      stmt.close();
    } catch (Exception ex) {
      System.out.println("$$$$$$$$WebLogicPool Connect Failed$$$$$");
      return false;
    }

    return true;
  }
  /**
   * 从WebSphere提供的连接池中取连接,失败返回false
   *
   * @return boolean
   */
  private boolean getWebSpherePoolConnection() {
    try {
      Context tContext = new InitialContext();
      // 如果在web.xml中声明了引用对象,则采用下面的方法
      DataSource tDataSource = (DataSource) tContext.lookup(JUrl.getDBName());
      // 下面的方法也可以发现到jndi数据
      //            DataSource tDataSource = (DataSource) tContext.lookup("jdbc/MET");
      // 不过会在日志中输出如下错误信息,websphere不建议采用
      //            [03-9-2 17:19:11:916 CST] 6b0e97e8 ConnectionFac I J2CA0122I: 无法定位资源引用
      // jdbc/db2ds,因此使用下列缺省值:[Resource-ref settings]
      //            res-auth:                 1 (APPLICATION)
      //            res-isolation-level:      0 (TRANSACTION_NONE)
      //            res-sharing-scope:        true (SHAREABLE)
      //            res-resolution-control:   999 (undefined)
      if (tDataSource != null) {
        con = tDataSource.getConnection();
        Statement stmt =
            con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        //       stmt.execute("alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'");
        stmt.close();

        if (con != null) {
          System.out.println("Connect succeed from websphere!");
          return true;
        } else {
          System.out.println("new Connection error ...");
          return false;
        }
      } else {
        System.out.println("new DataSource error ...");
        return false;
      }
    } catch (Throwable e) {
      System.out.println("look for jndi name error ...");
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 创建连接
   *
   * @return boolean
   */
  public boolean createConnection() {
    int dbType = 0;
    /**
     * WebLogic连接池配置调用 这里的方法,当从备份连接中读取得时候,没有把bIsPool重置为false 这样每次执行的就是con.close()方法,没有起到池的作用。
     * 而且如果是连接池已满而且没有空闲的连接,从备份中去连接就违背了规则。 dbtype=10没有意义
     */
    if (JUrl.getDBType().equalsIgnoreCase("WEBLOGICPOOL")) {
      dbType = 10;
      bIsPool = true;
      if (getWeblogicPoolConnection()) {
        return true;
      } else {
        // 当读取连接池失败时,从备份连接读取连接
        JdbcUrlBackUp tJdbcUrlBackUp = new JdbcUrlBackUp();
        JUrl.setDBName(tJdbcUrlBackUp.getDBName());
        JUrl.setDBType(tJdbcUrlBackUp.getDBType());
        JUrl.setIP(tJdbcUrlBackUp.getIP());
        JUrl.setPassWord(tJdbcUrlBackUp.getPassWord());
        JUrl.setPort(tJdbcUrlBackUp.getPort());
        JUrl.setServerName(tJdbcUrlBackUp.getServerName());
        JUrl.setUser(tJdbcUrlBackUp.getUserName());
      }
    }
    /** apache连接池配置调用 */
    else if (JUrl.getDBType().equalsIgnoreCase("COMMONSDBCP")) {
      bIsPool = true;
      if (getApachecommonDBCP()) {
        return true;
      } else {
        // 当读取连接池失败时,从备份连接读取连接
        JdbcUrlBackUp tJdbcUrlBackUp = new JdbcUrlBackUp();
        JUrl.setDBName(tJdbcUrlBackUp.getDBName());
        JUrl.setDBType(tJdbcUrlBackUp.getDBType());
        JUrl.setIP(tJdbcUrlBackUp.getIP());
        JUrl.setPassWord(tJdbcUrlBackUp.getPassWord());
        JUrl.setPort(tJdbcUrlBackUp.getPort());
        JUrl.setServerName(tJdbcUrlBackUp.getServerName());
        JUrl.setUser(tJdbcUrlBackUp.getUserName());
      }
    }
    /** WebSphere连接池配置调用 */
    else if (JUrl.getDBType().equalsIgnoreCase("WEBSPHERE")) {
      bIsPool = true;
      if (getWebSpherePoolConnection()) {
        return true;
      } else {
        // 当读取连接池失败时,从备份连接读取连接
        JdbcUrlBackUp tJdbcUrlBackUp = new JdbcUrlBackUp();
        JUrl.setDBName(tJdbcUrlBackUp.getDBName());
        JUrl.setDBType(tJdbcUrlBackUp.getDBType());
        JUrl.setIP(tJdbcUrlBackUp.getIP());
        JUrl.setPassWord(tJdbcUrlBackUp.getPassWord());
        JUrl.setPort(tJdbcUrlBackUp.getPort());
        JUrl.setServerName(tJdbcUrlBackUp.getServerName());
        JUrl.setUser(tJdbcUrlBackUp.getUserName());
        // ====ADD===zhangtao===2005-08-25=========BGN=================================

        System.out.println("========= WEBSPHERE Connect Pool Error Use BackUP ====");
        System.out.println("== DBName ======" + tJdbcUrlBackUp.getDBName());
        System.out.println("== DBType ======" + tJdbcUrlBackUp.getDBType());
        System.out.println("== IP ==========" + tJdbcUrlBackUp.getIP());
        System.out.println("== PassWord ====" + tJdbcUrlBackUp.getPassWord());
        System.out.println("== Port ========" + tJdbcUrlBackUp.getPort());
        System.out.println("== ServerName ==" + tJdbcUrlBackUp.getServerName());
        System.out.println("== UserName ====" + tJdbcUrlBackUp.getUserName());

        // ====ADD===zhangtao===2005-08-25=========END=================================
      }
    }
    /** 如果上面都没有执行成功,则调用自己编写的jdbc连接 */
    try {
      if (con != null) {
        if (!con.isClosed()) {
          try {
            // 为了解决“超时”的问题,在返回之前,先试用一下con
            Statement stmt = con.createStatement();
            stmt.execute("SELECT * FROM ledcode");
            stmt.close();
            return true;
          } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("DBConn : recreate DBConn");
            // 由于执行的sql编写规范不到位,导致很多非法sql描述。
            // 因此需要在这个地方捕获一下异常处理。
            try {
              con.close();
            } catch (Exception ex) {
              e.printStackTrace();
            } finally {
              con.close();
            }
            con = null;
          }
        }
        con = null;
      }

      // 判定数据库类型
      if (JUrl.getDBType().equalsIgnoreCase("ORACLE")) {
        dbType = 1;
      } else if (JUrl.getDBType().equalsIgnoreCase("INFORMIX")) {
        dbType = 2;
      } else if (JUrl.getDBType().equalsIgnoreCase("SQLSERVER")) {
        dbType = 3;
      } else if (JUrl.getDBType().equalsIgnoreCase("DB2")) {
        dbType = 4;
      } else if (JUrl.getDBType().equalsIgnoreCase("SYBASE")) {
        dbType = 5;
      }

      // 根据数据库类型动态加载驱动
      switch (dbType) {
        case 1:

          // ORACLE
          Class.forName("oracle.jdbc.driver.OracleDriver");
          break;
        case 2:

          // INFORMIX
          Class.forName("com.informix.jdbc.IfxDriver");
          break;
        case 3:

          // SQLSERVER
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          break;
        case 4:

          // DB2
          Class.forName("com.ibm.db2.jcc.DB2Driver");
          break;
        case 5:

          // SYBASE
          Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
          break;
        default:
          System.out.println("目前暂不支持此种类型的数据库!");
          return false;
      }
    } catch (Exception e) {
      return false;
    }
    // 尝试连接数据库
    try {
      switch (dbType) {
        case 1:

          // ORACLE
          // 不是很清楚下面的设置有什么含义的说
          // 好像一个是缓存取到的记录数,一个是设置默认的批量提交数
          Properties props = new Properties();
          props.setProperty("user", JUrl.getUserName());
          props.setProperty("password", JUrl.getPassWord());

          // 50的数量级好像已经很好的说,诡异
          props.setProperty("defaultRowPrefetch", "50");
          props.setProperty("defaultExecuteBatch", "50");
          con = DriverManager.getConnection(JUrl.getJdbcUrl(), props);

          //                    con = DriverManager.getConnection(JUrl.getJdbcUrl(),
          // JUrl.getUserName()
          //                            , JUrl.getPassWord());
          Statement stmt =
              con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
          //                    stmt.execute("alter session set nls_date_format = 'YYYY-MM-DD
          // HH24:MI:SS'");
          stmt.close();
          break;

        case 2:

          // INFORMIX
          con = DriverManager.getConnection(JUrl.getJdbcUrl());
          break;

        case 3:

          // SQLSERVER
          con =
              DriverManager.getConnection(
                  JUrl.getJdbcUrl(), JUrl.getUserName(), JUrl.getPassWord());
          break;
        case 4:

          // DB2
          con =
              DriverManager.getConnection(
                  JUrl.getJdbcUrl(), JUrl.getUserName(), JUrl.getPassWord());
          break;
        case 5:

          // SYBASE
          con =
              DriverManager.getConnection(
                  JUrl.getJdbcUrl(), JUrl.getUserName(), JUrl.getPassWord());
          break;
      }
    } catch (SQLException e) {
      System.out.println("创建连接失败...");
      return false;
    }
    return true;
  }