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包

   

阅读更多
个人分类: 数据库
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭