标签:change size static imp rar XML source nec 连接池
java
package com.library.util;
//导入c3p0包
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTools {
private static DataSource dataSource = new ComboPooledDataSource("testc3p0");
public static Connection getConnection(){
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void release(Connection connection, Statement statement, ResultSet resultSet){
try {
if (connection != null) connection.close();
if (statement !=null)statement.close();
if (resultSet!=null)resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="testc3p0">
<!-- 指定连接数据源的基本属性 -->
<property name="user">web</property>
<property name="password">123123</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/webtest?useUnicode=true&characterEncoding=UTF-8</property>
<!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize">5</property>
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>
标签:change size static imp rar XML source nec 连接池
原文地址:https://www.cnblogs.com/woshi123/p/12578172.html