基于 Apache DBCP 的数据库连接获取类

基于 Apache DBCP 的数据库连接获取类, 可以让你在 Tomcat 之外的 J2SE 程序或者其它应用服务器上使用 Apache 的数据库连接池.


TODO: 增加最大连接数和最小连接数的设置功能

配置文件:

ConnectionFactory.properties

# 2004-12-30
# 数据库连接工厂的配置文件, 类文件参见 util.ConnectionFactory
# 调试标志, 为 true 时使用 JDBC 直接连接(使用 url, driver, user, password 四个参数)
# 为 false 时使用内置数据库连接池(使用参数 jdbc.jndi), 配置文件位于 db.properties 中
debug=false
# JDBC的连接地址 URL
jdbc.url=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=yuelao
# JDBC 的驱动
jdbc.driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
# JDBC 的连接用户名
jdbc.user=sa
# JDBC 的连接密码
jdbc.password=
#debug 为 false 时, 将根据连接池名获取数据库连接, 适用于程序在服务器运行的时候


.Java 文件:

/*
* @(#)ConnectionFactory.java 1.2 2005-11-25
*
* Copyright 2005 BeanSoft Studio. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package beansoft.util;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;

import beansoft.jsp.StringUtil;
import beansoft.sql.DatabaseUtil;

/**
* ConnectionFactory provide a factory class that produces all database
* connections from here, and it provides methods for shutdown and restart
* data sources as well as reading and saving configuration parameters from/to file.
*
* 数据库连接工厂类, 所有的数据库连接都从这里产生, 提供关闭和重启数据源的方法, 以及读取和保存
* 配置参数到文件的能力.
*
* 2005-08-19
* Using Apache DBCP as database connection pool provider.
* 使用 Apache DBCP 作为连接池的提供类.
*
* @link http://jakarta.apache.org/commons/dbcp/
*
* Dependency:
* commons-collections.jar
* commons-pool.jar
* commons-dbcp.jar
* j2ee.jar (for the javax.sql classes)
*
* If you using this class with Tomcat's web application, then all the above jars
* not need to be added because the Tomcat it self has included these class libs.
* 如果你在 Tomcat 的 Web Application 中调用这个类, 以上的 JAR 都不用单独
* 加入的, 因为 Tomcat 默认已经自带了这些类库.
*
* @author BeanSoft
* @version 1.2
* 2005-11-25
*/
public class ConnectionFactory {

/** Database password */
private static String password;

/** Database username */
private static String user;

/** JDBC URL */
private static String url;

/**
* JDBC driver class name
*/
private static String driver;

/**
* DEBUG flag, default value is true, returns a connection that directly fetched using
* JDBC API; if falg is false, returns a connection returned by the connection pool,
* if u want depoly the application, please make this flag be false.
* 调试标记, 默认值为 true, 则返回直接使用 JDBC 获取的连接; 如果标记为 false,
* 则从连接池中返回器连接, 发布程序时请将这个标志 设置为 false.
*/
private static boolean DEBUG = true;

/** Connection properties value */
private static Properties props = new Properties();

/** The data source object, added at 2005-08-19 */
private static DataSource dataSource = null;

// Load configuration from resource /ConnectionFactory.properties
static {
loadConfiguration();
}

private ConnectionFactory() {
}

/**
* Factory method: obtain a database connection.
* 工厂方法: 获取一个数据库连接.
*
*
* @return Connection a java.sql.Connection object
*/
public static Connection getConnection() {
try {
Connection conn = null;

// Debug mode, obtain connection directly through JDBC API
if (DEBUG) {

Class.forName(driver);

conn = DriverManager.getConnection(url, user, password);
} else {
// TODO
// // Looking data source through JNDI tree
// DataSource dataSource = (DataSource) getInitialContext()
// .lookup(jndi);
// conn = dataSource.getConnection();
conn = setupDataSource().getConnection();
}

return conn;
} catch (Exception ex) {
System.err.println("Error: Unable to get a connection: " + ex);
ex.printStackTrace();
}

return null;
}

/**
* Load and parse configuration.
*/
public static void loadConfiguration() {
try {
props.load(new ByteArrayInputStream(readConfigurationString().getBytes()));

// Load DEBUG flag, default to true
DEBUG = Boolean.valueOf(props.getProperty("debug", "true"))
.booleanValue();
password = props.getProperty("jdbc.password", null);
user = props.getProperty("jdbc.user", null);
url = props.getProperty("jdbc.url", null);
driver = props.getProperty("jdbc.driver");
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Save the current configuration properties.
*/
public static void saveConfiguration() {
saveConfiguration(getProperties());
}


/**
* Read content string from configuration file. Because Class.getResourceAsStream(String)
* sometimes cache the contents, so here used this method.
* 读取配置文件中的字符串.
* 因为 Class 类的 getResourceAsStream(String) 方法有时候会出现缓存, 因此
* 不得已使用了这种办法.
* @return String, null if failed
*/
public static String readConfigurationString() {
try {
java.io.FileInputStream fin = new java.io.FileInputStream(
getConfigurationFilePath());
java.io.ByteArrayOutputStream bout = new
java.io.ByteArrayOutputStream();

int data;
while( (data = fin.read()) != -1) {
bout.write(data);
}
bout.close();
fin.close();

return bout.toString();
} catch (Exception ex) {
System.err.println("Unable to load ConnectionFactory.properties:" +
ex.getMessage());
ex.printStackTrace();
}
return null;
}

/**
* Get the configuration file's real physical path.
*/
private static String getConfigurationFilePath() {
return StringUtil.getRealFilePath("/ConnectionFactory.properties");
}

/**
* Save string content of a java.util.Properties object.
* 保存配置文件中的字符串.
*
* @param props configuration string
* @return operation result
*/
protected static boolean saveConfigurationString(String props) {
if(props == null || props.length() <= 0) return false;
try {
FileWriter out = new FileWriter(getConfigurationFilePath());
out.write(props);
out.close();

return true;
} catch (Exception ex) {
System.err.println("Unable save configuration string to ConnectionFactory.properties:"
+ ex);
ex.printStackTrace();
}

return false;
}

/**
* Returns the current database connection properties.
* @return Properties object
*/
public static Properties getProperties() {
return props;
}

/**
* Save configuration properties.
*
* @param props Properties
* @return operation result
*/
public static boolean saveConfiguration(Properties props) {
if(props == null || props.size() <= 0) return false;

try {
FileOutputStream out = new FileOutputStream(getConfigurationFilePath());
props.store(out, "");
out.close();

return true;
} catch (Exception ex) {
System.err.println("Unable to save ConnectionFactory.properties:" + ex.getMessage());
ex.printStackTrace();
}

return false;
}

/**
* Create a DataSource instance based on the Apache DBCP.
* 创建基于 Apache DBCP 的 DataSource.
*
* @return a poolable DataSource
*/
public static DataSource setupDataSource() {
if(dataSource == null) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(driver);
ds.setUsername(user);
ds.setPassword(password);
ds.setUrl(url);

dataSource = ds;
}

return dataSource;
}

/**
* Display connection status of current data source.
*
* 显示当前数据源的状态.
*/
public static String getDataSourceStats() {
BasicDataSource bds = (BasicDataSource) setupDataSource();
StringBuffer info = new StringBuffer();

info.append("Active connection numbers: " + bds.getNumActive());
info.append("/n");
info.append("Idle connection numbers: " + bds.getNumIdle());

return info.toString();
}

/**
* Shut down the data source, if want use it again,
* please call setupDataSource().
*/
public static void shutdownDataSource() {
BasicDataSource bds = (BasicDataSource) setupDataSource();
try {
bds.close();
} catch (SQLException e) {
// TODO auto generated try-catch
e.printStackTrace();
}
}

/**
* Restart the data source.
* 重新启动数据源.
*/
public static void restartDataSource() {
shutdownDataSource();
setupDataSource();
}

/** Test method */
public static void main(String[] args) {
Connection conn = ConnectionFactory.getConnection();

DatabaseUtil dbUtil = new DatabaseUtil();
dbUtil.setConnection(conn);

// try {
// java.sql.ResultSet rs = conn.createStatement().executeQuery(
// "SELECT MAX(ID) FROM items");
//
// while(rs.next()) {
// System.out.println(rs.getString(1));
// }
//
// rs.close();
//
// } catch (Exception ex) {
// ex.printStackTrace();
// }

System.out.println(dbUtil.getAllCount("SELECT MAX(ID) FROM items"));

System.out.println(conn);

try {
conn.close();
} catch (Exception ex) {
// ex.printStackTrace();
}

conn = ConnectionFactory.getConnection();

System.out.println(conn);

try {
conn.close();
} catch (Exception ex) {
// ex.printStackTrace();
}

System.exit(0);
}

}

posted on 2007-01-19 11:04 BeanSoft 阅读(2424) 评论(1)   编辑   收藏 所属分类: Database
<script type="text/javascript"> // </script>
#  re: 基于 Apache DBCP 的数据库连接获取类(原创)
小车马
Posted @ 2007-01-23 23:38
恩,以前也用过,apache的很多东西都非常不错,

楼主,潜力贴论坛( http://content.uu1001.com)是我个人的一个设想,如果你对java非常的专注,并且愿意交我这个朋友,可以发邮件给我(lbw070105@gmail.com),希望我们可以一起发展它。   回复   更多评论    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值