`

dbcp 学习笔记

阅读更多
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dbcp;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

//
// Here are the dbcp-specific classes.
// Note that they are only used in the setupDriver
// method. In normal use, your classes interact
// only with the standard JDBC API
//
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;

//
// Here's a simple example of how to use the PoolingDriver.
// In this example, we'll construct the PoolingDriver manually,
// just to show how the pieces fit together, but you could also
// configure it using an external conifguration file in
// JOCL format (and eventually Digester).
//

//
// To compile this example, you'll want:
//  * commons-pool-1.5.4.jar
//  * commons-dbcp-1.2.2.jar
// in your classpath.
//
// To run this example, you'll want:
//  * commons-collections.jar
//  * commons-pool-1.5.4.jar
//  * commons-dbcp-1.2.2.jar
//  * the classes for your (underlying) JDBC driver
// in your classpath.
//
// Invoke the class using two arguments:
//  * the connect string for your underlying JDBC driver
//  * the query you'd like to execute
// You'll also want to ensure your underlying JDBC driver
// is registered.  You can use the "jdbc.drivers"
// property to do this.
//
// For example:
//  java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver \
//       -classpath commons-pool-1.5.3.jar:commons-dbcp-1.2.2.jar:oracle-jdbc.jar:. \
//       ManualPoolingDriverExample \
//       "jdbc:oracle:thin:scott/tiger@myhost:1521:mysid" \
//       "SELECT * FROM DUAL"
//
public class ManualPoolingDriverExample {

    public static void main(String[] args) {
        //
        // First we load the underlying JDBC driver.
        // You need this if you don't use the jdbc.drivers
        // system property.
        //
        System.out.println("Loading underlying JDBC driver.");
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("Done.");

        //
        // Then we set up and register the PoolingDriver.
        // Normally this would be handled auto-magically by
        // an external configuration, but in this example we'll
        // do it manually.
        //
        System.out.println("Setting up driver.");
        try {
            setupDriver(args[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Done.");

        //
        // Now, we can use JDBC as we normally would.
        // Using the connect string
        //  jdbc:apache:commons:dbcp:example
        // The general form being:
        //  jdbc:apache:commons:dbcp:<name-of-pool>
        //

        Connection conn = null;
        Statement stmt = null;
        ResultSet rset = null;

        try {
            System.out.println("Creating connection.");
            conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
            System.out.println("Creating statement.");
            stmt = conn.createStatement();
            System.out.println("Executing statement.");
            rset = stmt.executeQuery(args[1]);
            System.out.println("Results:");
            int numcols = rset.getMetaData().getColumnCount();
            while(rset.next()) {
                for(int i=1;i<=numcols;i++) {
                    System.out.print("\t" + rset.getString(i));
                }
                System.out.println("");
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            try { if (rset != null) rset.close(); } catch(Exception e) { }
            try { if (stmt != null) stmt.close(); } catch(Exception e) { }
            try { if (conn != null) conn.close(); } catch(Exception e) { }
        }

        // Display some pool statistics
        try {
            printDriverStats();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // closes the pool
        try {
            shutdownDriver();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setupDriver(String connectURI) throws Exception {
        //
        // First, we'll need a ObjectPool that serves as the
        // actual pool of connections.
        //
        // We'll use a GenericObjectPool instance, although
        // any ObjectPool implementation will suffice.
        //
        ObjectPool connectionPool = new GenericObjectPool(null);

        //
        // Next, we'll create a ConnectionFactory that the
        // pool will use to create Connections.
        // We'll use the DriverManagerConnectionFactory,
        // using the connect string passed in the command line
        // arguments.
        //
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);

        //
        // Now we'll create the PoolableConnectionFactory, which wraps
        // the "real" Connections created by the ConnectionFactory with
        // the classes that implement the pooling functionality.
        //
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);

        //
        // Finally, we create the PoolingDriver itself...
        //
        Class.forName("org.apache.commons.dbcp.PoolingDriver");
        PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");

        //
        // ...and register our pool with it.
        //
        driver.registerPool("example",connectionPool);

        //
        // Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
        // to access our pool of Connections.
        //
    }

    public static void printDriverStats() throws Exception {
        PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        ObjectPool connectionPool = driver.getConnectionPool("example");
        
        System.out.println("NumActive: " + connectionPool.getNumActive());
        System.out.println("NumIdle: " + connectionPool.getNumIdle());
    }

    public static void shutdownDriver() throws Exception {
        PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        driver.closePool("example");
    }
}
0
0
分享到:
评论

相关推荐

    DBCP 数据库连接池JNDI连接 学习笔记

    NULL 博文链接:https://housheng33.iteye.com/blog/1522705

    开发工具 commons-dbcp2-2.1.1

    开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发...

    dbcp jar包 dbcp jar 包

    dbcp jar包 一个是dbcp的包, 一个是pool包, 两者都导入工程

    创建dbcp连接,dbcp(Spring)

    创建dbcp连接,dbcp(Spring)

    commons-dbcp2-2.9.0-bin.zip

    DBCP(DataBase Connection Pool)是 apache common上的一个 java 连接池项目,也是 tomcat 使用的连接池组件,依赖 于Jakarta commons-pool 对象池机制,DBCP可以直接的在应用程序中使用。 使用DBCP会用到commons-...

    DBCP依赖Jar包

    DBCP的依赖Jar包,完整的,亲测能用,欢迎下载!DBCP的依赖Jar包,完整的,亲测能用,欢迎下载!

    commons-dbcp-1.4-API文档-中英对照版.zip

    赠送jar包:commons-dbcp-1.4.jar; 赠送原API文档:commons-dbcp-1.4-javadoc.jar; 赠送源代码:commons-dbcp-1.4-sources.jar; 赠送Maven依赖信息文件:commons-dbcp-1.4.pom; 包含翻译后的API文档:commons-...

    DBCP连接池DBCP和C3P0配置

    DBCP连接池DBCP和C3P0配置,可以对数据源进行各种有效的控制

    commons中的DBCP连接池jar

    commons中的DBCP连接池jar,用于利用dbcp链接数据库

    DBCP连接池原理分析

    DBCP连接池介绍 ----------------------------- 目前 DBCP 有两个版本分别是 1.3 和 1.4。 DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ,支持 JDBC 3。 DBCP 1.4 版本需要运行于 JDK 1.6 ,支持 JDBC 4。 1.3和1.4基于...

    dbcp所需要jar

    SpringMVC链接mysql数据库,配置需要用到的两个包class="org.apache.commons.dbcp.BasicDataSource

    commons-dbcp-1.2.2

    commons-dbcp-1.2.2 commons-dbcp-1.2.2 commons-dbcp-1.2.2

    commons-dbcp.jar.rar

    commons-dbcp.jar dbcp数据库连接池驱动,包括pool、logging两个依赖

    DBCP数据库连接池包下载

    DBCP

    commons-dbcp-1.4

    commons-dbcp-1.4,到官网下载慢到抽筋,传这里来了。

    commons-dbcp

    commons-dbcp-1.3-javadoc.jar, commons-dbcp-1.3-RC1.jar, commons-dbcp-1.3-sources.jar, commons-dbcp-1.3.jar, commons-dbcp-1.4-javadoc.jar, commons-dbcp-1.4-sources.jar, commons-dbcp-1.4.jar, commons-...

    commons-dbcp-1.4.jar

    DBCP是Apache提供的一款开源免费的数据库连接池!Hibernate3.0之后不再对DBCP提供支持!因为Hibernate声明DBCP有致命的缺欠!DBCP因为Hibernate的这一毁谤很是生气,并且说自己没有缺欠。

    DBCP1.4.chm

    DBCP1.4.chm帮助文档,更好地理解和掌握dbcp数据库连接池。

    commons-dbcp2-2.7.0.jar

    commons-dbcp2-2.7.0.jar用于Java连接数据库的使用,方便操作,简化代码,对于新入手学习JDBC的朋友可以尝试使用,idea 用DBCP连接数据库时必备jar包

    DBCP(数据库连接池)

    DBCP 数据库连接池 DBCP 数据库连接池 DBCP 数据库连接池 里面是DBCP的jar,导进去就可以用了

Global site tag (gtag.js) - Google Analytics