C3P0数据库连接池
1.写c3p0-config.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
<!-- Mysql数据库连接的设置 -->
<default-config>
<!--Mysql数据库的驱动类-->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!--链接数据库的字符串-->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test3</property>
<!--数据库的用户名-->
<property name="user">root</property>
<!--数据库的密码-->
<property name="password">990128</property>
<!-- 初始化连接数量 -->
<property name="initialPoolSize">10</property>
<!-- 最大的连接延迟 -->
<property name="maxIdleTime">30</property>
<!-- 最大连接数量10 和 最小连接数量1 -->
<property name="maxPoolSize">10</property>
<property name="minPoolSize">1</property>
</default-config>
</c3p0-config>
2.写C3P0的工具类(需要导包)
1.导包C3P0的jar包
链接:https://pan.baidu.com/s/1YLliDdzLCoFZnhpBrQdUSg
提取码:sut2
细节:补充一个jar包,DButils框架的jar包,可以简化数据库操作的代码!!!
链接:https://pan.baidu.com/s/1nGPT0JbVwKmm97tzyIUbsQ
提取码:qosv
2.导入C3P0的jar包,写工具类
3.测试
1.总的工具类代码
package util;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.*;
/**
*
* @author wh
*/
public class C3P0Util {
/**得到数据源*/
private static DataSource dataSource = new ComboPooledDataSource();
/**
* 使用xml配置文件
* todo 从数据源(连接池)获取连接对象
*/
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("系统繁忙");
}
}
/**关闭连接*/
public static void close(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 这样配置依赖太强了,不推荐
* 使用xml文件配置
*/
/**
* static {
* try {
* //Mysql数据库连接
* dataSource.setDriverClass("com.mysql.jdbc.Driver");//
* dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test3");
* dataSource.setUser("root");
* dataSource.setPassword("990128");
* //初始化10个连接
* dataSource.setInitialPoolSize(10);
* } catch (PropertyVetoException e) {
* e.printStackTrace();
* }
* <p>
* }
*/
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = C3P0Util.getConnection();
System.out.println("连接:"+connection);
//
String sql = "insert into dataSource(driverClass,jdbcURL,user,password)" +
" values(?,?,?,?)";
//预编译
statement = connection.prepareStatement(sql);
//参数赋值
statement.setString(1, "com.mysql.jdbc.Driver");
statement.setString(2, "jdbc:mysql://localhost:3306/test3");
statement.setString(3, "root");
statement.setString(4, "123");
//执行
statement.executeUpdate();
System.out.println("执行成功!");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
//关闭资源
C3P0Util.close(statement, connection);
}
}
}