目录
1.首先要创建一个xml,其名字一定是c3p0-config.xml(代码中自动寻找)
1、配置文件 c3p0.properties(名字不是固定)
1、首先要导入c3p0依赖
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>
jdbc依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
2.xml形式
1.首先要创建一个xml,其名字一定是c3p0-config.xml(代码中自动寻找)
<c3p0-config>
<!--使用默认的配置读取数据库连接池对象 -->
<default-config>
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/website?serverTimezone=Asia/Shanghai</property>
<property name="user">root</property>
<property name="password">123456</property>
<!-- 连接池参数 -->
<!--初始化申请的连接数量-->
<property name="initialPoolSize">5</property>
<!--最大的连接数量-->
<property name="maxPoolSize">10</property>
<!--超时时间-->
<property name="checkoutTimeout">3000</property>
</default-config>
<!-- <named-config name="otherc3p0">-->
<!-- <!– 连接参数 –>-->
<!-- <property name="driverClass">com.mysql.jdbc.Driver</property>-->
<!-- <property name="jdbcUrl">jdbc:mysql://localhost:3306/hs_test?serverTimezone=Asia/Shanghai</property>-->
<!-- <property name="user">root</property>-->
<!-- <property name="password">root</property>-->
<!-- <!– 连接池参数 –>-->
<!-- <property name="initialPoolSize">5</property>-->
<!-- <property name="maxPoolSize">8</property>-->
<!-- <property name="checkoutTimeout">1000</property>-->
<!-- </named-config>-->
</c3p0-config>
2.测试
//1.创建数据库连接池对象
DataSource ds = new ComboPooledDataSource();
//2. 获取连接对象
Connection conn = ds.getConnection();后面的代码和jdbc就接通了
public static void main(String[] args) throws SQLException {
DataSource dataSource = new ComboPooledDataSource();
Connection conn = dataSource.getConnection();
String sql="select * from user";
PreparedStatement pre=conn.prepareStatement(sql);
ResultSet res= pre.executeQuery();
while(res.next()){
System.out.print(res.getString(1)+" ");
System.out.println(res.getString(2));
}
}
3、结果
查询出数据库的数据,不做展示
3.配置文件形式
1、配置文件 c3p0.properties(名字不是固定)
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/website?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
2、测试代码(读取文件的方法)
ComboPooledDataSource ds = new ComboPooledDataSource();
ResourceBundle rb = ResourceBundle.getBundle("c3p0");
ds.setDriverClass(rb.getString("driverClass"));
ds.setJdbcUrl(rb.getString("url"));
ds.setUser(rb.getString("username"));
ds.setPassword(rb.getString("password"));
ds.setInitialPoolSize(Integer.parseInt(rb.getString("initialSize")));
ds.setMaxPoolSize(Integer.parseInt(rb.getString("maxActive")));
ds.setCheckoutTimeout(Integer.parseInt(rb.getString("maxWait")));
Connection conn=ds.getConnection();
String sql="select * from user";
PreparedStatement pre=conn.prepareStatement(sql);
ResultSet res=pre.executeQuery();
while(res.next()){
System.out.print(res.getString(1)+" ");
System.out.println(res.getString(2));
}
conn.close();
pre.close();
res.close();