C3P0 Java 数据库系列
1、C3P0基本概念。
如果不知道什么是线程池,可以百度一下,我理解的呢,就是一个大水池,里面有很多鱼,每条鱼对应一个数据库连接,
你拿到一条鱼你就获得一个连接,当然,这鱼不能吃,你摸完了还要放回去的。这例子有点变态哈哈,能理解就行。
所需jar包:c3p0-0.9.2.1.jar,mchange-commons-java-0.2.3.4.jar,mysql-connector-java-5.1.40-bin.jar;
2、C3P0的三种配置方式。
c3p0的配置方式分为三种,分别是
1.类路径下提供一个c3p0-config.xml文件
2.类路径下提供一个c3p0.properties文件
3.setters一个个地设置各个配置项
2.1、xml文件配置
使用读取配置文件的方式,要求是, 配置文件必须命名为c3p0-config.xml,并且放在src目录下 ,配置文件如下:
- <span style="font-size:14px;"><?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</property>
- <property name="driverClass">com.mysql.jdbc.Driver</property>
- <property name="checkoutTimeout">30000</property>
- <property name="idleConnectionTestPeriod">30</property>
- <property name="initialPoolSize">10</property>
- <property name="maxIdleTime">30</property>
- <property name="maxPoolSize">100</property>
- <property name="minPoolSize">10</property>
- <property name="maxStatements">200</property>
- <user-overrides user="test-user">
- <property name="maxPoolSize">10</property>
- <property name="minPoolSize">1</property>
- <property name="maxStatements">0</property>
- </user-overrides>
- </default-config>
- <!-- 命名的配置 -->
- <named-config name="test">
- <property name="user">root</property>
- <property name="password">123456</property>
- <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
- <property name="driverClass">com.mysql.jdbc.Driver</property>
- <!-- 如果池中数据连接不够时一次增长多少个 -->
- <property name="acquireIncrement">5</property>
- <!-- 初始化数据库连接池时连接的数量 -->
- <property name="initialPoolSize">20</property>
- <!-- 数据库连接池中的最大的数据库连接数 -->
- <property name="maxPoolSize">25</property>
- <!-- 数据库连接池中的最小的数据库连接数 -->
- <property name="minPoolSize">5</property>
- </named-config>
- </c3p0-config></span>
- <span style="font-size:14px;">package com.wqc.util;
- import java.sql.Connection;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- public class C3P0Utils {
- /*
- //这个是默认配置的
- private static Connection conn;
- private static ComboPooledDataSource ds = new ComboPooledDataSource();
- public static Connection getConnection() {
- try {
- conn = ds.getConnection();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (PropertyVetoException e) {
- e.printStackTrace();
- }
- return conn;
- }*/
- //定义全局变量
- private static ComboPooledDataSource cpds;
- //静态代码块,命名配置
- static {
- cpds = new ComboPooledDataSource("test");
- }
- //获取数据源
- public static DataSource getDataSource() {
- return cpds;
- }
- //获取连接
- public static Connection getConnection() throws SQLException {
- return cpds.getConnection();
- }
- }</span>
2.2、c3p0.properties文件配置
使用读取属性文件的方式,要求是, 属性文件必须命名为c3p0.properties,(注意:名字是固定的,c3p0默认只认识这个名字)并且放在src目录下 ,配置文件如下:- <span style="font-size:14px;">c3p0.driverClass=com.mysql.jdbc.Driver
- c3p0.jdbcUrl=jdbc:mysql://localhost:3306/test
- c3p0.user=root
- c3p0.password=123456</span>
- <span style="font-size:14px;">package com.wqc.test;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- public class C3P0Utils {
- public void getConnection() throws SQLException {
- DataSource ds = new ComboPooledDataSource();//c3p0自己去读配置文件了,我们啥也不干
- Connection con = ds.getConnection();//我们直接拿一条鱼
- System.out.println("con:"+con);
- String sql = "select * from user";
- PreparedStatement ps = con.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- System.out.println(rs.getString(1));
- System.out.println(rs.getString(2));
- System.out.println(rs.getString(3));
- System.out.println(rs.getString(5));
- System.out.println(rs.getString(6));
- System.out.println(rs.getString(7));
- }
- //关闭连接
- con.close();//这是把连接还给连接池,而不是关闭连接
- rs.close();
- ps.close();
- }
- }</span>
- <span style="font-size:14px;">jdbcdriver=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/test
- username=root
- password=123465</span>
- package com.wqc.test;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Properties;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- public class C3P0Utils {
- public void getConnection() throws SQLException {
- Properties prop = new Properties();
- InputStream in = C3P0Utils.class.getClassLoader().getResourceAsStream("db.properties");
- prop.load(in);//加载信息
- ComboPooledDataSource comb = new ComboPooledDataSource();//下面就是开始配置
- comb.setDriverClass(prop.getProperty("jdbcdriver"));
- comb.setJdbcUrl(prop.getProperty("url"));
- comb.setUser(prop.getProperty("jdbc:mysql://localhost:3306/test"));
- comb.setPassword(prop.getProperty("root"));
- Connection con = comb.getConnection();//从c3p0拿一条鱼,啊呸!什么鱼啊,拿一条数据库连接
- //执行查询语句
- String sql = "select * from user";
- PreparedStatement ps = con.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
- //打印数据库信息
- while(rs.next())
- {
- System.out.println(rs.getString(1));
- System.out.println(rs.getString(2));
- System.out.println(rs.getString(3));
- System.out.println(rs.getString(5));
- System.out.println(rs.getString(6));
- System.out.println(rs.getString(7));
- }
- //关闭连接
- con.close();//这是把连接还给连接池,而不是关闭连接
- rs.close();
- ps.close();
- }
- }