c3p0-config.xml
<c3p0-config>
<named-config name="c3p0">
<property name="user">root</property>
<property name="password">hello</property>
<property name="url">jdbc:mysql://localhost:3306/frienddb</property>
<property name="driver">com.mysql.jdbc.Driver</property>
<!--数据库连接池连接数不足时,向数据库服务器申请的连接数-->
<property name="acquireIncrement">50</property>
<!--初始化连接数 -->
<property name="initialPoolSize">1</property>
<!-- 最小连接数-->
<property name="minPoolSize">5</property>
<!-- 最大连接数-->
<property name="maxPoolSize">50</property>
<!-- 数据库连接池可以维护的statement个数-->
<property name="maxStatements">5</property>
<!-- 每个连接同时可以使用的statement个数-->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
package general.page.query;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Tool {
private static ComboPooledDataSource cpds=null;
//数据库连接池只被初始化一次
static{
cpds = new ComboPooledDataSource("c3p0");
}
public static Connection getConnection() throws SQLException{
return cpds.getConnection();
}
public static void close(ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(PreparedStatement prst){
if(prst!=null){
try {
prst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(PreparedStatement prst,Connection conn){
close(prst);
close(conn);
}
public static void close(ResultSet rs,PreparedStatement prst,Connection conn){
close(rs);
close(prst);
close(conn);
}
}