关闭

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

2401人阅读 评论(0) 收藏 举报

 

 

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

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:111590次
    • 积分:1205
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:6篇
    • 译文:0篇
    • 评论:13条