C3P0資料庫連接池使用,

來源:互聯網
上載者:User

C3P0資料庫連接池使用,

C3P0資料庫連接池使用

1、拷貝jar包:c3p0-0.9.1.2.jar     c3p0-0.9.1.2-jdk1.3.jar    c3p0-oracle-thin-extras-0.9.1.2.jar(oracle需要)

2、書寫配製檔案放在src目錄下:c3p0-config.xml(名字只能是這個)

3、類C3P0Util

c3p0-config.xml 內容:

<?xml version="1.0" encoding="UTF-8"?><c3p0-config><!-- This is default config! --><default-config><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></default-config><!-- This is my config for mysql--><named-config name="mysql"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property><property name="user">root</property><property name="password"></property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></named-config><!-- This is my config for oracle --><named-config name="oracle"><property name="driverClass">oracle.jdbc.driver.OracleDriver</property><property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property><property name="user">scott</property><property name="password">liang</property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></named-config></c3p0-config>

C3P0Util:

package com.liang.util;import java.io.InputStream;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Properties;import com.mchange.v2.c3p0.ComboPooledDataSource;/** * 資料庫工具類 * @author liang * */public class C3P0Util {static ComboPooledDataSource cpds=null;static{//這裡有個優點,寫好設定檔,想換資料庫,簡單//cpds = new ComboPooledDataSource("oracle");//這是oracle資料庫cpds = new ComboPooledDataSource("mysql");//這是mysql資料庫}/** * 獲得資料庫連接 * @return   Connection */public static Connection getConnection(){try {return cpds.getConnection();} catch (SQLException e) {e.printStackTrace();return null;}}/** * 資料庫關閉操作 * @param conn   * @param st     * @param pst * @param rs */public static void close(Connection conn,PreparedStatement pst,ResultSet rs){if(rs!=null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if(pst!=null){try {pst.close();} catch (SQLException e) {e.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}/** * 測試DBUtil類 * @param args */public static void main(String[] args) {Connection conn=getConnection();System.out.println(conn.getClass().getName());close(conn,null,null);}}








相關關鍵詞:
相關文章

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.