代码改变世界

Java c3p0连接池之二

2016-12-27 19:03  甘雨路  阅读(155)  评论(0编辑  收藏  举报
<?xml version="1.0" encoding="UTF-8"?>

<!-- c3p0-config.xml文件配置 -->

<c3p0-config>
    <named-config name="bookstore_c3p0">
        <!-- 基本配置 -->
        <property name="user">LF</property>
        <property name="password">LF</property>
        <property name="jdbcUrl">jdbc:oracle:thin:@192.168.10.105:1521:orcl</property>
        <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
        <!-- 配置初始值 -->
        <property name="initialPoolSize">16</property>
        <property name="maxPoolSize">25</property>
        <property name="minPoolSize">10</property>
    </named-config>
</c3p0-config>
package com.lf.bookstore.utils;


import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtils {
    public static void main(String[] args) {
        
        System.out.println(JdbcUtils.getConnection());
    }

    /**
     * 获取数据库连接
     * @return
     */
    public static Connection getConnection() {
        DataSource ds = new ComboPooledDataSource("bookstore_c3p0");
        Connection connection = null;
        try {
            connection = ds.getConnection();
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}