配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="data">
<property name="user">scott</property>
<property name="password">123456</property>
<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<property name="jdbcUrl">jdbc:oracle:thin:@192.168.32.32:1521:orcl</property>
<property name="initialPoolSize">3</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">20</property>
<property name="minPoolSize">3</property>
<property name="maxIdleTimeExcessConnections">15</property>
</named-config>
</c3p0-config>
实现类:
package com.niit.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.PoolBackedDataSource;
/**
* 数据库连接类工具
* @author Administrator
*
*/
public class UtilsSql {
private static UtilsSql install = new UtilsSql();
private static ComboPooledDataSource data;
private UtilsSql(){
data = new ComboPooledDataSource("data");
}
/**
* 获取连接
* @return 连接
* @throws SQLException 连接异常
*/
public static Connection getConnection() throws SQLException{
return data.getConnection();
}
public static UtilsSql getUtilsSql(){
return install;
}
/**
* 关闭连接
*/
public static void closeAll(Connection conn, Statement ment, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ment != null) {
try {
ment.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}