JDBC的一个连接池,在我们项目中用到的。

原创 2004年10月30日 15:12:00
<script type="text/javascript"><!-- google_ad_client = "pub-2016433482135581"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_channel =""; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

package dbconnection;

import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.DbUtils;

public class DBConnection implements Serializable{
  private java.lang.String dbUserID;
  private java.lang.String dbPassword;
  private java.lang.String dbDriver;
  private java.lang.String dbURL;
  private java.lang.String sqlString;
  private java.sql.ResultSet result;
  private java.sql.ResultSetMetaData meta;
  private java.sql.Connection conn;
  private java.sql.Statement sqlStmt;
  private java.lang.String drcName;
  private final static java.lang.String className = "dbconnection.DBConnection";
  private java.lang.String initialContext;
  /**
   * DBConnection constructor comment.
   */
  public DBConnection (){
    super ();
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/31/00 3:44:45 PM)
   */
  public void close (){
    String methodName = "close()";
    DBConnection.closeResultSetAndStatement (sqlStmt, result);
    DBConnection.closeConnection (conn);
  }

  /**
   * Insert the method's description here.
   * Creation date: (7/25/2003 5:34:40 PM)
   */
  public void closeStatement (){
    try{
      if (sqlStmt != null){
        sqlStmt.clearBatch ();
        sqlStmt.clearWarnings ();
        sqlStmt.close ();
      }
    } catch (SQLException e){
    }
  }

  /**
   * Insert the method's description here.
   * Creation date: (11/2/00 10:51:57 AM)
   * @exception java.sql.SQLException The exception description.
   */
  public void commit (){
    String methodName = "commit()";
    try{
      if (conn != null && !conn.isClosed ()){
        conn.commit ();
      }
    } catch (SQLException e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
    }
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/6/00 4:04:34 PM)
   */
  public void connect (){
    String methodName = "connect()";
    try{
      if (conn == null){
        if (!registerDriver (dbDriver)){
          throw new SQLException ("Can't load JDBC Driver - " + dbDriver + ".");
        }
        /*
                 InitialContext context = new InitialContext ();
                 DataSource ds = (DataSource)context.lookup (drcName);
         */
        DataSource ds = setupDataSource ();
        Connection connection = ds.getConnection ();
        conn = connection;
      }
    } catch (SQLException e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
    }
    /*
        catch (NamingException e){
         System.out.println (className + "." + methodName + " ==> " +
                        e.getClass ().getName () + ": " + e.getMessage ());
        }
     */
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/16/00 2:17:02 PM)
   * @return dbconnection.DBConnection
   */
  public static DBConnection dbconnect (){

    String methodName = "dbconnect(String, String)";
    DBConnection dbconn = null;

    try{
      //   initialize dbconnection
      dbconn = new DBConnection ();

//  Get DB connection parameters from central getDBConnectionParm bean
      getParm getDBConnectionParm = new getParm ();
      getDBConnectionParm.init ();

      dbconn.setDbUserID (getDBConnectionParm.getParameter ("userID"));
//  out.println("After setDbUserID");
      dbconn.setDbPassword (getDBConnectionParm.getParameter ("password"));
      dbconn.setDbURL (getDBConnectionParm.getParameter ("URL"));
      dbconn.setDbDriver (getDBConnectionParm.getParameter ("driver"));
      dbconn.setDrcName (getDBConnectionParm.getParameter (
          "ConnectionPoolLocation"));
      dbconn.setInitialContext (getDBConnectionParm.getParameter (
          "initial_context"));
      System.out.println (dbconn.getDbUserID () + ", " + dbconn.getDbPassword () +
                          ", " + dbconn.getDbURL () + ", " + dbconn.getDbDriver () +
                          ", " + dbconn.getDrcName () + ", " +
                          dbconn.getInitialContext ());
      dbconn.connect ();
      System.out.println ("After dbconn.connect()");
    } catch (SQLException e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
    } catch (Exception e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
      e.printStackTrace ();
    }
    return dbconn;
  }

  public static DBConnection dbconnect (String filename){

    String methodName = "dbconnect(String, String)";
    DBConnection dbconn = null;

    try{
      //   initialize dbconnection
      dbconn = new DBConnection ();

//  Get DB connection parameters from central getDBConnectionParm bean
      getParm getDBConnectionParm = new getParm ();
      getDBConnectionParm.init (filename);

      dbconn.setDbUserID (getDBConnectionParm.getParameter ("userID"));
//  out.println("After setDbUserID");
      dbconn.setDbPassword (getDBConnectionParm.getParameter ("password"));
      dbconn.setDbURL (getDBConnectionParm.getParameter ("URL"));
      dbconn.setDbDriver (getDBConnectionParm.getParameter ("driver"));
      dbconn.setDrcName (getDBConnectionParm.getParameter (
          "ConnectionPoolLocation"));
      dbconn.setInitialContext (getDBConnectionParm.getParameter (
          "initial_context"));
      System.out.println (dbconn.getDbUserID () + ", " + dbconn.getDbPassword () +
                          ", " + dbconn.getDbURL () + ", " + dbconn.getDbDriver () +
                          ", " + dbconn.getDrcName () + ", " +
                          dbconn.getInitialContext ());
      dbconn.connect ();
      System.out.println ("After dbconn.connect()");
    } catch (SQLException e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
    } catch (Exception e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
    }
    return dbconn;
  }

  /**
   * This method have Response Object to allow you to print some information on the browser.
   * Creation date: (9/29/00 4:35:14 PM)
   */
  private void execute (String sqltype) throws SQLException{

    if (conn != null && !conn.isClosed ()){
      sqlStmt = conn.createStatement ();
      sqlStmt.executeQuery (getSqlString ());
      if (sqltype.equals ("s") || sqltype.equals ("S")){
        result = sqlStmt.getResultSet ();
        meta = result.getMetaData ();
      }
    }

  }

  /**
   * This method have Response Object to allow you to print some information on the browser.
   * Creation date: (9/29/00 4:35:14 PM)
   */
  public void execute (String sqlString, String sqlType) throws SQLException{

    this.sqlString = sqlString;
    execute (sqlType);
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/6/00 4:04:06 PM)
   * @return java.sql.Connection
   */
  public java.sql.Connection getConn (){
    return conn;
  }

  /**
   * return the driver of db
   * Creation date: (9/29/00 4:29:45 PM)
   * @return java.lang.String
   */
  public java.lang.String getDbDriver (){
    return dbDriver;
  }

  /**
   * return the password of db
   * Creation date: (9/29/00 4:29:25 PM)
   * @return java.lang.String
   */
  public java.lang.String getDbPassword (){
    return dbPassword;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 4:34:01 PM)
   * @return java.lang.String
   */
  public java.lang.String getDbURL (){
    return dbURL;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 4:29:04 PM)
   * @return java.lang.String
   */
  public java.lang.String getDbUserID (){
    return dbUserID;
  }

  /**
   * Insert the method's description here.
   * Creation date: (7/17/2002 12:17:51 PM)
   * @return java.lang.String
   */
  public java.lang.String getDrcName (){
    return drcName;
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/8/02 10:07:36 AM)
   * @return java.lang.String
   */
  public java.lang.String getInitialContext (){
    return initialContext;
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/2/00 1:35:04 PM)
   * @return java.sql.ResultSetMetaData
   */
  public java.sql.ResultSetMetaData getMeta (){
    return meta;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 5:05:13 PM)
   * @return java.sql.ResultSet
   */
  public java.sql.ResultSet getResult (){
    return result;
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/6/00 4:06:40 PM)
   * @return java.sql.Statement
   */
  public java.sql.Statement getSqlStmt (){
    return sqlStmt;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 4:55:48 PM)
   * @return java.lang.String
   */
  public java.lang.String getSqlString (){
    return sqlString;
  }

  /**
   * Insert the method's description here.
   * Creation date: (7/18/02 5:05:30 PM)
   */
  public void rollback (){
    try{
      if (conn != null && !conn.isClosed ()){
        conn.rollback ();
      }
    } catch (SQLException e){
    }
  }

  /**
   * Insert the method's description here.
   * Creation date: (7/18/02 5:04:34 PM)
   * @param autoCommit boolean
   */
  public void setAutoCommit (boolean autoCommit){
    String methodName = "setAutoCommit()";
    try{
      if (conn != null && !conn.isClosed ()){
        conn.setAutoCommit (autoCommit);
      }
    } catch (SQLException e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
    }
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/6/00 4:04:06 PM)
   * @param newConn java.sql.Connection
   */
  public void setConn (java.sql.Connection newConn){
    conn = newConn;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 4:29:45 PM)
   * @param newDbDriver java.lang.String
   */
  public void setDbDriver (java.lang.String newDbDriver){
    dbDriver = newDbDriver;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 4:29:25 PM)
   * @param newDbPassword java.lang.String
   */
  public void setDbPassword (java.lang.String newDbPassword){
    dbPassword = newDbPassword;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 4:34:01 PM)
   * @param newDbURL java.lang.String
   */
  public void setDbURL (java.lang.String newDbURL){
    dbURL = newDbURL;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 4:29:04 PM)
   * @param newDbUserID java.lang.String
   */
  public void setDbUserID (java.lang.String newDbUserID){
    dbUserID = newDbUserID;
  }

  /**
   * Insert the method's description here.
   * Creation date: (7/17/2002 12:17:51 PM)
   * @param newDrcName java.lang.String
   */
  public void setDrcName (java.lang.String newDrcName){
    drcName = newDrcName;
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/8/02 10:07:36 AM)
   * @param newInitialContext java.lang.String
   */
  public void setInitialContext (java.lang.String newInitialContext){
    initialContext = newInitialContext;
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/2/00 1:35:04 PM)
   * @param newMeta java.sql.ResultSetMetaData
   */
  public void setMeta (java.sql.ResultSetMetaData newMeta){
    meta = newMeta;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 5:05:13 PM)
   * @param newResult java.sql.ResultSet
   */
  void setResult (java.sql.ResultSet newResult){
    result = newResult;
  }

  /**
   * Insert the method's description here.
   * Creation date: (10/6/00 4:06:40 PM)
   * @param newSqlStmt java.sql.Statement
   */
  public void setSqlStmt (java.sql.Statement newSqlStmt){
    sqlStmt = newSqlStmt;
  }

  /**
   * Insert the method's description here.
   * Creation date: (9/29/00 4:55:48 PM)
   * @param newSqlString java.lang.String
   */
  private void setSqlString (java.lang.String newSqlString){
    sqlString = newSqlString;
  }

  public DataSource setupDataSource (){
    BasicDataSource ds = new BasicDataSource ();
    ds.setDriverClassName (dbDriver);
    ds.setUsername (dbUserID);
    ds.setPassword (dbPassword);
    ds.setUrl (dbURL);
    return ds;
  }

  public boolean registerDriver (String driverName){
    return DbUtils.loadDriver (driverName);
  }

  public static void closeResultSetAndStatement (java.sql.ResultSet rs){
    String methodName = "closeResultSetAndStatement(java.sql.ResultSet)";
    java.sql.Statement stmt = null;
    try{
      stmt = rs.getStatement ();
    } catch (java.sql.SQLException e){
      System.err.println ("com.netstarmacau.sampleorder.util.DbConnection.closeResultSetAndStatement(Resultset) ==> " +
                          e.getMessage ());
    } finally{
      closeResultSetAndStatement (stmt, rs);
    }
  }

  public static void closeResultSetAndStatement (java.sql.Statement stmt){
    String methodName = "closeResultSetAndStatement(java.sql.Statement)";
    java.sql.ResultSet rs = null;
    try{
      rs = stmt.getResultSet ();
    } catch (java.sql.SQLException e){
      System.err.println ("com.netstarmacau.sampleorder.util.DbConnection.closeResultSetAndStatement(Resultset) ==> " +
                          e.getMessage ());
    } finally{
      closeResultSetAndStatement (stmt, rs);
    }
  }

  public static void closeResultSetAndStatement (java.sql.Statement stmt,
                                                 java.sql.ResultSet rs){
    String methodName =
        "closeResultSetAndStatement(java.sql.Statement, ava.sql.ResultSet)";
    closeResultSet (rs);
    closeStatement (stmt);
  }

  public static void closeStatement (java.sql.Statement stmt){
    DbUtils.closeQuietly (stmt);
  }

  public static void closeResultSet (java.sql.ResultSet rs){
    DbUtils.closeQuietly (rs);
  }

  public static void closeConnection (Connection conn){
    DbUtils.closeQuietly (conn);
  }

  public static void closeAll (Connection conn, Statement stmt, ResultSet rs){
    DbUtils.closeQuietly (conn, stmt, rs);
  }

  public static void closeAll (DBConnection dbconn, Statement stmt,
                               ResultSet rs){
    if (dbconn != null){
      dbconn.close ();
    }
    closeResultSetAndStatement (stmt, rs);
  }

  public static void commit (Connection conn){
    String methodName = "commit()";
    try{
      if (conn != null && !conn.isClosed ()){
        conn.commit ();
      }
    } catch (SQLException e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
    }
  }

  public static void setAutoCommit (Connection conn, boolean autoCommit){
    String methodName = "setAutoCommit()";
    try{
      if (conn != null && !conn.isClosed ()){
        conn.setAutoCommit (autoCommit);
      }
    } catch (SQLException e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
    }
  }

  public static void rollback (Connection conn){
    String methodName = "rollback()";
    try{
      if (conn != null && !conn.isClosed ()){
        conn.rollback ();
      }
    } catch (SQLException e){
      System.out.println (className + "." + methodName + " ==> " +
                          e.getClass ().getName () + ": " + e.getMessage ());
    }
  }

  public static void closePrePareStatement (java.sql.PreparedStatement pstmt){
    DbUtils.closeQuietly (pstmt);
  }

}

在项目中使用springmvc

什么是springmvcspringmvc是spring框架的一个模块,springmvc和spring无需通过中间整个层进行整合,它是一个基于mvc的web框架。springmvc与struts2的...
  • jinzili777
  • jinzili777
  • 2016年04月09日 08:36
  • 1371

一个项目中用到的50个SQL语句

这是CSDN论坛里面的一个帖子,作者是爱新觉罗.毓华。感觉比较全面,就整理下来了,留作以后参考备用。在此,在此感谢本文的作者。/*标题:一个项目涉及到的50个Sql语句(整理版)作者:爱新觉罗.毓华(...
  • tangbin330
  • tangbin330
  • 2010年05月19日 15:06
  • 2230

Java - 你在项目中哪些地方用到了XML?

XML的主要作用有两个方面:数据交换和信息配置。在做数据交换时,XML将数据用标签组装成起来,然后压缩打包加密后通过网络传送给接收者,接收解密与解压缩后再从XML文件中还原相关信息进行处理,XML曾经...
  • chimomo
  • chimomo
  • 2017年11月01日 10:13
  • 255

项目中使用到的技术

盒子汽车Redis使用场景生成不重复订单号(数据类型-字符串):/** * 返回周期内从1~n计数数值 * 如:$range=y表示本年内从1开始计数,$range=d表示本月内从...
  • zzq156532
  • zzq156532
  • 2017年03月23日 17:58
  • 134

项目中缓存的使用技巧总结

1.对于ibatis\hibenate等数据库框架的自带缓存在BI项目中运用时qis
  • huwenhu2007
  • huwenhu2007
  • 2014年05月04日 22:05
  • 2284

项目中使用到的设计模式

1、模板方法模式 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中,如JdbcTemplate 2、代理 spring的Proxy模式在aop中有体现 3、观察者 定义对象间的一种一对多...
  • luwei42768
  • luwei42768
  • 2017年02月05日 20:22
  • 624

SpringAOP在项目中的简单应用

 SpringAOP在项目中的简单应用 一、前言 项目背景 由于公司的机房到期,需要做应用迁移(包括数据库),割接到云平台。但是又不能进行一次性割接,需要分三个阶段:第一阶段割...
  • keyingbo2008
  • keyingbo2008
  • 2014年04月03日 21:47
  • 3960

关于项目中线程池的使用

进入京东以来接触的一个框架叫做 worker框架,其实质上就是一个任务调度框架,实质上就是在quartz定时任务框架上进行了一层封装,主要用于后台任务的定时的调度。...
  • dsc2015
  • dsc2015
  • 2016年10月04日 23:11
  • 813

在实际项目中用UML建模的总结

做机房收费系统的时候,刚对UML有一个大致的概念,就开始了对机房收费系统的建模。        UML可以理解为一个分析交流的方法。多个人建一个系统,人多了,为了避免歧义,就得用标准化的交流方法。图...
  • xqf309
  • xqf309
  • 2012年06月10日 08:12
  • 2821

做项目中自己使用的java设计模式-单例设计模式

做项目时使用的单例设计模式
  • chenchudongsg
  • chenchudongsg
  • 2014年04月07日 22:31
  • 539
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JDBC的一个连接池,在我们项目中用到的。
举报原因:
原因补充:

(最多只允许输入30个字)