Cp0数据库连接
- 配置文件
- 工具包
架包准备
1、c3p0-config.xml
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8
</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">3</property>
<property name="maxPoolSize">6</property>
<property name="maxIdleTime">1000</property>
</default-config>
<named-config name="oracle_config">
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_demo</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">scot</property>
<property name="password">tiger</property>
<property name="initialPoolSize">3</property>
<property name="maxPoolSize">6</property>
<property name="maxIdleTime">1000</property>
</named-config>
</c3p0-config>
2、C3p0工具类
public class c3p0Utils {
private static ComboPooledDataSource dataSource=null;
static {
dataSource=new ComboPooledDataSource();
}
public static Connection getConn(){
Connection conn=null;
try {
conn= dataSource.getConnection();
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void closeRes(ResultSet rs, PreparedStatement stmt,Connection conn){
if (!Objects.isNull(rs)){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (!Objects.isNull(stmt)){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (!Objects.isNull(conn)){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}