`
kwstartw
  • 浏览: 68103 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java 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.
*/
import javax.sql.DataSource;
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 setupDataSource
// method. In normal use, your classes interact
// only with the standard JDBC API
//
import org.apache.commons.dbcp.BasicDataSource;
//
// Here's a simple example of how to use the BasicDataSource.
// In this example, we'll construct the BasicDataSource manually,
// but you could also configure it using an external conifguration file.
//
//
// Note that this example is very similiar to the PoolingDriver
// example.
//
// To compile this example, you'll want:
// * commons-pool-1.3.jar
// * commons-dbcp-1.2.2.jar
// * j2ee.jar (for the javax.sql classes)
// in your classpath.
//
// To run this example, you'll want:
// * commons-pool-1.3.jar
// * commons-dbcp-1.2.2.jar
// * j2ee.jar (for the javax.sql classes)
// * 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.3.jar:commons-dbcp-1.2.2.jar:j2ee.jar:oracle-jdbc.jar:. \
// ManualPoolingDataSourceExample
// "jdbc:oracle:thin:scott/tiger@myhost:1521:mysid"
// "SELECT * FROM DUAL"
//
public class BasicDataSourceExample {
public static void main(String[] args) {
// First we set up the BasicDataSource.
// 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 data source.");
DataSource dataSource = setupDataSource(args[0]);
System.out.println("Done.");
//
// Now, we can use JDBC DataSource as we normally would.
//
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
System.out.println("Creating connection.");
conn = dataSource.getConnection();
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 { rset.close(); } catch(Exception e) { }
try { stmt.close(); } catch(Exception e) { }
try { conn.close(); } catch(Exception e) { }
}
}
public static DataSource setupDataSource(String connectURI) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUsername("scott");
ds.setPassword("tiger");
ds.setUrl(connectURI);
return ds;
}
public static void printDataSourceStats(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
System.out.println("NumActive: " + bds.getNumActive());
System.out.println("NumIdle: " + bds.getNumIdle());
}
public static void shutdownDataSource(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
bds.close();
}
}

 

分享到:
评论

相关推荐

    java dbcp连接池

    dbcp连接池

    java DBCP连接池需要jar

    DBCP(DataBase Connection Pool)数据库连接池,是java数据库连接池的一种,由Apache开发,通过数据库连接池,可以让程序自动管理数据库连接的释放和断开 亲测可用,用于java DBCP的链接必须jar包

    java配置dbcp连接池(数据库连接池)示例分享

    java配置dbcp连接池示例分享,大家参考使用吧

    DBCP连接池的jar包

    DBCP连接池的jar包,欢迎下载

    mysql8 DBCP连接池jar依赖

    用于实现DBCP连接池所用的JAR依赖文件,包括数据库驱动及创建连接池所需的其他依赖: * commons-collections  * commons-dbcp2  * commons-logging  * commons-pool2  * mysql-connector

    dbcp连接池使用例子

    一个dbcp连接池的使用例子,包含jar包,配有说明文档

    java配置dbcp数据库连接池架包

    里面有java配置dbcp连接池的所有架包

    利用dbcp实现数据库连接池

    利用dbcp实现数据库连接池,附带两个jar包,本人已测试通过

    dbcp数据库连接池jar包

    java web开发 dbcp数据库连接池 所用jar包。java web开发 dbcp数据库连接池 所用jar包。

    DBCP连接池所需完整架包(全)

    DBCP(DataBase connection pool),数据库连接池。是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要2个包:commons-dbcp.jar,commons-pool.jar由于建立数据库连接是一个非常耗时耗...

    dbcp连接池和配置文件

    内含jar文件,附加上了配置信息,有重要单词,供java开发者使用

    dbcp连接池.rar

    dbcp连接池用于java连接数据库

    java数据库连接池dbcp

    java数据库连接池的开源版,dbcp,很好用,经过测试后个人觉得比较好配置。

    Dbcp连接池-Java项目-3个jar包

    dbcp连接池的三个jar包,commons-logging-1.2.jar,commons-dbcp2-2.1.1.jar,commons-pool2-2.4.2.jar,以及jar包的Apache官网下载地址,压缩包内有3个zip包,需要解压得到3个jar包。

    DBCP连接池所需Jar包

    DBCP连接池,需要用到的Jar包

    dbcp连接池所需jar包

    commons-dbcp-1.4.jar commons-dbcp-1.4-bin.zip commons-dbcp-1.4-src.zip commons-dbutils-1.4.jar commons-pool-1.5.6.jar commons-pool-1.5.6-bin.zip commons-pool-1.5.6-src.zip commons-pool-1.6.jar mysql-...

    dbcp数据库连接池使用jar包

    DBCP(DataBase Connection Pool)数据库连接池,是java数据库连接池的一种,由Apache开发,通过数据库连接池,可以让程序自动管理数据库连接的释放和断开。

    DBCP资源池使用jar包

    是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。 单独使用dbcp需要3个包:common-dbcp.jar,common-pool.jar,common-collections.jar 由于建立数据库连接是一个非常耗时耗资源的行为,所以...

    dbcp连接池依赖包.rar

    commons-dbcp-1.4.jar,commons-pool-1.5.6.jar DBCP(DataBase Connection Pool)数据库连接池,是java数据库连接池的一种

    DBCP数据库连接池1.2jar包

    是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要3个包:commons-dbcp.jar,commons-pool.jar,commons-collections.jar由于建立数据库连接是一个非常耗时耗资源的行为,所以通过...

Global site tag (gtag.js) - Google Analytics