使用c3p0需要导包!
提取码:1234https://pan.baidu.com/s/1_ApzPbBVYe0eciLsH6aDaw
为什么用连接池?
数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现的尤为突出。对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标。数据库连接池负责分配,管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是重新建立一个。
数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”。预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去。我们可以通过设定连接池最大连接数来防止系统无尽的与数据库连接。更为重要的是我们可以通过连接池的管理机制监视数据库的连接的数量﹑使用情况,为系统开发﹑测试及性能调整提供依据。
配置c3p0的三种方法
法一(set方法写死在代码中)
代码:
package day1013;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.*;
public class a_c3p0_normalSet {
// (1)通过set方法写死在程序中。
// 固定格式
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
public static void configDataSource() {
// 配置dataSource
try {
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc_test?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai");
dataSource.setUser("root");
dataSource.setPassword("123456");
dataSource.setInitialPoolSize(3); //连接池最开始放几个链接(设置初始连接)
dataSource.setMaxPoolSize(10);
dataSource.setMinPoolSize(3);
dataSource.setAcquireIncrement(3); //连接数不够用时,单次添加的数量。
// 以上单独写一个方法
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() {
// 获取连接
Connection conn = null;
configDataSource(); //使用配置连接池方法
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) throws PropertyVetoException, SQLException, InterruptedException {
// 1.通过c3p0连接池获取数据库的连接。
Connection connection = getConnection();
// 2.设置SQL语句。
String sql = "select * from car";
// 3.创建Statement对象用来执行SQL语句。
PreparedStatement preparedStatement = connection.prepareStatement(sql);
// 4.创建ResultSet对象,获取结果集。
ResultSet resultSet = preparedStatement.executeQuery();
// 5.处理结果集。
// 获取所有字段名字
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int countCols = resultSetMetaData.getColumnCount();
// 将获取到的字段用循环输出,注意:(下标是从1开始)
for (int i = 1; i <= countCols; i++) {
System.out.print(resultSetMetaData.getColumnName(i));
System.out.print("\t");
}
System.out.println();
// 处理内容(记录指向结果集的最开始)
resultSet.beforeFirst();
while (resultSet.next()) {
System.out.print(resultSet.getString("id") + "\t");
System.out.print(resultSet.getString("name") + "\t");
System.out.println(resultSet.getString("producer") + "\t");
}
// 输出查看连接池中的数据
System.out.println("被占用的连接数" + dataSource.getNumBusyConnections());
System.out.println("连接池中的总的连接数" + dataSource.getNumConnections());
// 6.关闭连接,将连接交给连接池。注意:不是释放资源
connection.close();
Thread.sleep(1000);
System.out.println("被占用的连接数" + dataSource.getNumBusyConnections());
System.out.println("连接池中的总的连接数" + dataSource.getNumConnections());
}
}
该代码创建了两个方法,其实是可用写在main方法里面的,但是在实际写代码中,代码功能能写在方法里肯定是最好是写在方法里的。
法一(set方法写死在代码中)与普通通过字符串配置jdbc差不多,唯一不同的是增加了对数量池的配置。
法二(通过c3po-config.xml文件配置)
在src目录下创建c3p0-config.xml文件,注意文件名必须与此名一致,不可更改。配置文件中,可以有多个数据库的连接配置。
<default-config></default-config>包裹的内容是默认的数据库配置,如果不在代码中指定使用哪个配置,那就使用默认配置。
<named-config name="配置名称"></named-config>包裹的内容为特定的数据库配置,可以在代码中通过配置名称指定要采用哪个数据库配置。
<property name="属性名称">配置内容</property>包裹的内容为指定属性的配置。
idea创建.xml文件(这个创建完还需重命名改为xml后缀,并把原始内容删掉):
c3p0-config.xml文件内容:
.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
<!-- 默认配置,如果没有指定则使用这个配置 -->
<default-config>
<property name="user">root</property>
<property name="password">123456</property>
<property name="jdbcUrl">
jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
<!-- jdbc:mysql://localhost:3306/jdbc_test?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai-->
</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">3</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">2</property>
<property name="maxStatements">200</property>
</default-config>
<!-- 命名了的配置,可以通过方法调用实现 -->
<named-config name="mySource">
<property name="user">root</property>
<property name="password">123456</property>
<property name="jdbcUrl">
jdbc:mysql://localhost:3306/jdbc_test?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 如果池中数据连接不够时一次增长多少个 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize">50</property>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize">5</property>
</named-config>
</c3p0-config>
代码图片:
除了图片部分,其他内容跟法一一样。
法三(通过c3p0.properties文件配置)
跟普通的properties是一样的。
c3p0.properties文件内容:
.properties代码:
#jdbc配置文件
#1.注册数据库驱动
DriverClass=com.mysql.jdbc.Driver
#2.JDBC连接串
JdbcUrl=jdbc:mysql://localhost:3306/jdbc_test?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
#3.数据库用户名
User=root
#4.数据库密码
Password=123456
#5.最大连接数
MaxPoolSize=20
#6.最小连接数
MinPoolSize=2
#7.初始连接数
InitialPoolSize=5
#8.新开连接步长数
acquireIncrement=5
#9.用以控制数据源内加载的PreparedStatement数量
MaxStatements =30
#10.最大空闲时间
MaxIdleTime=100
#11.连接超时值
CheckoutTimeout=3000
代码照片:
代码:
package day1013;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.*;
import java.util.ResourceBundle;
public class e_c3p0_properties {
//(3)通过c3p0.properties文件配置
private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); //自动找参数
public static void configDataSource() {
// 配置dataSource
try {
ResourceBundle bundle = ResourceBundle.getBundle("c3p0"); // 不带扩展名
dataSource.setDriverClass(bundle.getString("DriverClass"));
dataSource.setJdbcUrl(bundle.getString("JdbcUrl"));
dataSource.setUser(bundle.getString("User"));
dataSource.setPassword(bundle.getString("Password"));
dataSource.setInitialPoolSize(Integer.parseInt(bundle.getString("InitialPoolSize"))); //连接池最开始放几个链接(设置初始连接)
dataSource.setMaxPoolSize(Integer.parseInt(bundle.getString("MaxPoolSize")));
dataSource.setMinPoolSize(Integer.parseInt(bundle.getString("MinPoolSize")));
dataSource.setAcquireIncrement(Integer.parseInt(bundle.getString("acquireIncrement"))); //连接数不够用时,单次添加的数量。
// 以上单独写一个方法
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() {
// 获取连接
Connection conn = null;
configDataSource(); //使用配置连接池方法
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) throws PropertyVetoException, SQLException, InterruptedException {
// 1.通过c3p0连接池获取数据库的连接
Connection connection = getConnection();
// 2.设置SQL语句。
String sql = "select * from car";
// 3.创建Statement对象用来执行SQL语句。
PreparedStatement preparedStatement = connection.prepareStatement(sql);
// 4.创建ResultSet对象,获取结果集。
ResultSet resultSet = preparedStatement.executeQuery();
// 5.处理结果集。
// 获取所有字段名字
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int countCols = resultSetMetaData.getColumnCount();
// 将获取到的字段用循环输出,注意:(下标是从1开始)
for (int i = 1; i <= countCols; i++) {
System.out.print(resultSetMetaData.getColumnName(i));
System.out.print("\t");
}
System.out.println();
// 处理内容(记录指向结果集的最开始)
resultSet.beforeFirst();
while (resultSet.next()) {
System.out.print(resultSet.getString("id") + "\t");
System.out.print(resultSet.getString("name") + "\t");
System.out.println(resultSet.getString("producer") + "\t");
}
// 输出查看连接池中的数据
System.out.println("被占用的连接数" + dataSource.getNumBusyConnections());
System.out.println("连接池中的总的连接数" + dataSource.getNumConnections());
// 6.关闭连接,将连接交给连接池。注意:不是释放资源
connection.close();
Thread.sleep(1000);
System.out.println("被占用的连接数" + dataSource.getNumBusyConnections());
System.out.println("连接池中的总的连接数" + dataSource.getNumConnections());
}
}