java類調用第三方串連池 (c3p0)

來源:互聯網
上載者:User

 

 

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包

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.