java第三方连接池_java类调用第三方连接池 (c3p0)

1、首先编写db_cn.properties属性文件

DB.DRIVER=oracle.jdbc.driver.OracleDriver

DB.URL=jdbc/:oracle/:thin/:@192.168.1.162/:1521/:ora10g

DB.USER=new_lottery

DB.PWD=new_lottery

DB.AcquireIncrement=1

DB.InitialPoolSize=1

DB.MaxPoolSize=3

DB.MinPoolSize=1

DB.MaxIdleTime=120

DB.MaxStatements=180

DB.IdleConnectionTestPeriod=3600

存储数据库连接的相关配置

2、编写测试java-----DataHandling类。

package com.diagram.basis.common.db;

import java.sql.Connection;

import java.util.Date;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import com.diagram.basis.common.util.CFG;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**

* 数据处理工具类

*

* @author Administrator

*/

public class DataHandling {

private static Log log = LogFactory.getLog("DataHandling");

private static CFG cfg = new CFG();

/**

* C3P连接池对象

*/

private static ComboPooledDataSource cpds = null;

/**

* 数据库驱动名称

*/

private static final String DB_DRIVER = cfg.getMessage("DB.DRIVER", "/db_cn.properties");

/**

* 数据库连接URL

*/

private static final String DB_URL = cfg.getMessage("DB.URL", "/db_cn.properties");

/**

* 数据库连接用户

*/

private static final String DB_USER = cfg.getMessage("DB.USER", "/db_cn.properties");

/**

* 数据库连接密码

*/

private static final String DB_PWD = cfg.getMessage("DB.PWD", "/db_cn.properties");

/**

* 缺省构造函数

*/

static {

cpds = getInstance();

}

/**

* @return ComboPooledDataSource

*/

private synchronized static ComboPooledDataSource getInstance() {

if (cpds == null) {

cpds = new ComboPooledDataSource();

cpds.setAcquireIncrement(new Integer(cfg.getMessage("DB.AcquireIncrement", "/db_cn.properties")).intValue());

cpds.setInitialPoolSize(new Integer(cfg.getMessage("DB.InitialPoolSize", "/db_cn.properties")).intValue());

cpds.setMaxPoolSize(new Integer(cfg.getMessage("DB.MaxPoolSize", "/db_cn.properties")).intValue());

cpds.setMinPoolSize(new Integer(cfg.getMessage("DB.MinPoolSize", "/db_cn.properties")).intValue());

cpds.setMaxIdleTime(new Integer(cfg.getMessage("DB.MaxIdleTime", "/db_cn.properties")).intValue());

try {

cpds.setDriverClass(DB_DRIVER);

} catch (Exception e) {

log.error("setdriverclass error", e);

}

cpds.setJdbcUrl(DB_URL);

cpds.setUser(DB_USER);

cpds.setPassword(DB_PWD);

cpds.setMaxStatements(new Integer(cfg.getMessage("DB.MaxStatements", "/db_cn.properties")).intValue());

cpds.setIdleConnectionTestPeriod(new Integer(cfg.getMessage("DB.IdleConnectionTestPeriod", "/db_cn.properties")).intValue());

}

return cpds;

}

/**

* 取得数据库的连接

*

* @return Connection

* @throws Exception

*/

public Connection getConnection() throws Exception {

Connection connection = null;

try {

Date before = new Date();

connection = cpds.getConnection();

Date after = new Date();

log.info("-------time costed: "

+ (after.getTime() - before.getTime()));

// 设置非自动提交模式

connection.setAutoCommit(false);

} catch (Exception e) {

log.error("getconnection error", e);

throw e;

}

return connection;

}

public static void main(String[] args) {

DataHandling datahandling = new DataHandling();

Connection con = null;

int i = 0;

while (i < 50) {

java.util.Date before = new java.util.Date();

try {

con = datahandling.getConnection();

} catch (Exception e) {

e.printStackTrace();

}

try {

con.close();

} catch (Exception e) {

e.printStackTrace();

}

java.util.Date after = new java.util.Date();

log.info("-------time costed: "

+ (after.getTime() - before.getTime()));

i++;

}

}

}

3、读取属性文件的帮助类CFG.java

/*

* shining

* */

package com.diagram.basis.common.util;

import java.io.IOException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.util.Properties;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**

* @author  黄波涛

* @version 1.1

*

*/

public class CFG {

private static Log log = LogFactory.getLog(CFG.class);

private static IO iostream = new IO();

private static StrReplace strreplace = new StrReplace();

/**

* @integer 读取KEY值

* @param   key

* @return

*/

public String getMessage(String key) {

String message = null;

Properties properties = new Properties();

InputStream is = getClass().getResourceAsStream(

"/language_cn.properties");

try {

properties.load(is);

} catch (IOException e) {

log.error("getMessage() fail", e);

}

message = properties.getProperty(key);

try {

message = new String(message.getBytes("iso-8859-1"), "utf-8");

} catch (UnsupportedEncodingException e) {

log.error(e);

}

return message;

}

/**

* @integer 读取指定文件的KEY值

* @param   key

* @param   uri

* @return

*/

public String getMessage(String key,String uri) {

String message = null;

Properties properties = new Properties();

InputStream is = getClass().getResourceAsStream(

uri);

try {

properties.load(is);

} catch (IOException e) {

log.error("getMessage() fail", e);

}

message = properties.getProperty(key);

try {

message = new String(message.getBytes("iso-8859-1"), "utf-8");

} catch (UnsupportedEncodingException e) {

log.error(e);

}

return message;

}

/**

* @integer 读取指定文件的KEY值,并设定编码方式。

* @param   key

* @param   uri

* @param   encoding

* @return

*/

public String getMessage(String key,String uri,String encoding) {

String message = null;

Properties properties = new Properties();

InputStream is = getClass().getResourceAsStream(

uri);

try {

properties.load(is);

} catch (IOException e) {

log.error("getMessage() fail", e);

}

message = properties.getProperty(key);

try {

message = new String(message.getBytes("iso-8859-1"), encoding);

} catch (UnsupportedEncodingException e) {

log.error(e);

}

return message;

}

public Integer put(String key,String value){

Properties properties = new Properties();

InputStream is = getClass().getResourceAsStream(

"/language_cn.properties");

try {

properties.load(is);

//iostream.writeFile(uri, filename, suffix, filecontent);

} catch (IOException e) {

log.error("getMessage() fail", e);

return -1; //返回1表示失败

}

return 0; //返回0表示成功

}

public static void main(String[] args){

}

}

4 、加入c3p0包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值