C3P0 Java 数据库系列

1、C3P0基本概念。

C3P0是一个开源的JDBC 连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的 开源项目有Hibernate,Spring等。
如果不知道什么是线程池,可以百度一下,我理解的呢,就是一个大水池,里面有很多鱼,每条鱼对应一个 数据库连接,
你拿到一条鱼你就获得一个连接,当然,这鱼不能吃,你摸完了还要放回去的。这例子有点变态哈哈,能理解就行。
所需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目录下 ,配置文件如下:

 
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <c3p0-config>  
  3. <!-- 默认配置,如果没有指定则使用这个配置 -->  
  4.     <default-config>  
  5.         <property name="user">root</property>  
  6.         <property name="password">123456</property>  
  7.         <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>  
  8.         <property name="driverClass">com.mysql.jdbc.Driver</property>  
  9.         <property name="checkoutTimeout">30000</property>  
  10.         <property name="idleConnectionTestPeriod">30</property>  
  11.         <property name="initialPoolSize">10</property>  
  12.         <property name="maxIdleTime">30</property>  
  13.         <property name="maxPoolSize">100</property>  
  14.         <property name="minPoolSize">10</property>  
  15.         <property name="maxStatements">200</property>  
  16.         <user-overrides user="test-user">  
  17.             <property name="maxPoolSize">10</property>  
  18.             <property name="minPoolSize">1</property>  
  19.             <property name="maxStatements">0</property>  
  20.         </user-overrides>  
  21.     </default-config>  
  22.     <!-- 命名的配置 -->  
  23.     <named-config name="test">  
  24.         <property name="user">root</property>  
  25.         <property name="password">123456</property>  
  26.         <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>  
  27.         <property name="driverClass">com.mysql.jdbc.Driver</property>  
  28.         <!-- 如果池中数据连接不够时一次增长多少个 -->  
  29.         <property name="acquireIncrement">5</property>  
  30.         <!-- 初始化数据库连接池时连接的数量 -->  
  31.         <property name="initialPoolSize">20</property>  
  32.         <!-- 数据库连接池中的最大的数据库连接数 -->  
  33.         <property name="maxPoolSize">25</property>  
  34.         <!-- 数据库连接池中的最小的数据库连接数 -->  
  35.         <property name="minPoolSize">5</property>  
  36.     </named-config>  
  37. </c3p0-config></span>  
获取connection的java代码为:

 
  1. <span style="font-size:14px;">package com.wqc.util;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.SQLException;  
  5. import javax.sql.DataSource;  
  6. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  7.   
  8. public class C3P0Utils {  
  9.     /* 
  10.     //这个是默认配置的 
  11.     private static Connection conn; 
  12.     private static ComboPooledDataSource ds = new ComboPooledDataSource(); 
  13.  
  14.     public static Connection getConnection() { 
  15.         try { 
  16.             conn = ds.getConnection(); 
  17.         } catch (SQLException e) { 
  18.             e.printStackTrace(); 
  19.         } catch (PropertyVetoException e) { 
  20.             e.printStackTrace(); 
  21.         } 
  22.         return conn; 
  23.     }*/  
  24.     //定义全局变量  
  25.     private static ComboPooledDataSource cpds;  
  26.     //静态代码块,命名配置  
  27.     static {  
  28.         cpds = new ComboPooledDataSource("test");  
  29.     }  
  30.     //获取数据源  
  31.     public static DataSource getDataSource() {  
  32.         return cpds;  
  33.     }  
  34.     //获取连接  
  35.     public static Connection getConnection() throws SQLException {  
  36.         return cpds.getConnection();  
  37.     }  
  38.       
  39. }</span>  
获取到Connection对象后就可以通过类似Apache的DbUtils这样的工具类(或者原生的Preparedstatement)来访问操作数据库。

2.2、c3p0.properties文件配置

使用读取属性文件的方式,要求是, 属性文件必须命名为c3p0.properties,(注意:名字是固定的,c3p0默认只认识这个名字)并且放在src目录下 ,配置文件如下:

 
  1. <span style="font-size:14px;">c3p0.driverClass=com.mysql.jdbc.Driver  
  2. c3p0.jdbcUrl=jdbc:mysql://localhost:3306/test  
  3. c3p0.user=root  
  4. c3p0.password=123456</span>  
获取connection对象的代码:

 
 
  1. <span style="font-size:14px;">package com.wqc.test;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import javax.sql.DataSource;  
  8. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  9.   
  10. public class C3P0Utils {  
  11.     public void getConnection() throws SQLException {  
  12.         DataSource ds = new ComboPooledDataSource();//c3p0自己去读配置文件了,我们啥也不干  
  13.         Connection con = ds.getConnection();//我们直接拿一条鱼  
  14.         System.out.println("con:"+con);  
  15.   
  16.         String sql = "select * from user";  
  17.         PreparedStatement ps = con.prepareStatement(sql);  
  18.         ResultSet rs = ps.executeQuery();  
  19.            
  20.         while(rs.next())  
  21.         {  
  22.             System.out.println(rs.getString(1));  
  23.             System.out.println(rs.getString(2));  
  24.             System.out.println(rs.getString(3));  
  25.             System.out.println(rs.getString(5));  
  26.             System.out.println(rs.getString(6));  
  27.             System.out.println(rs.getString(7));   
  28.         }  
  29.         //关闭连接  
  30.         con.close();//这是把连接还给连接池,而不是关闭连接  
  31.         rs.close();  
  32.         ps.close();  
  33.     }  
  34. }</span>  
 
 
 
 
 

2.3、setters一个个地设置各个配置项

通过读取properties配置文件。然后获取c3p0的连接。

首先我们准备一个db.properties

 
  1. <span style="font-size:14px;">jdbcdriver=com.mysql.jdbc.Driver  
  2. url=jdbc:mysql://localhost:3306/test  
  3. username=root  
  4. password=123465</span>  
获取Connection对象的Java代码:

 
  1. package com.wqc.test;  
  2.   
  3. import java.io.InputStream;  
  4. import java.sql.Connection;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.util.Properties;  
  9.   
  10. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  11.   
  12. public class C3P0Utils {  
  13.     public void getConnection() throws SQLException {  
  14.         Properties prop = new Properties();  
  15.         InputStream in = C3P0Utils.class.getClassLoader().getResourceAsStream("db.properties");  
  16.         prop.load(in);//加载信息  
  17.            
  18.         ComboPooledDataSource comb = new ComboPooledDataSource();//下面就是开始配置  
  19.         comb.setDriverClass(prop.getProperty("jdbcdriver"));  
  20.         comb.setJdbcUrl(prop.getProperty("url"));  
  21.         comb.setUser(prop.getProperty("jdbc:mysql://localhost:3306/test"));  
  22.         comb.setPassword(prop.getProperty("root"));  
  23.            
  24.         Connection con = comb.getConnection();//从c3p0拿一条鱼,啊呸!什么鱼啊,拿一条数据库连接  
  25.         //执行查询语句  
  26.         String sql = "select * from user";  
  27.         PreparedStatement ps = con.prepareStatement(sql);  
  28.         ResultSet rs = ps.executeQuery();  
  29.         //打印数据库信息  
  30.         while(rs.next())  
  31.         {  
  32.             System.out.println(rs.getString(1));  
  33.             System.out.println(rs.getString(2));  
  34.             System.out.println(rs.getString(3));  
  35.             System.out.println(rs.getString(5));  
  36.             System.out.println(rs.getString(6));  
  37.             System.out.println(rs.getString(7));   
  38.         }  
  39.         //关闭连接  
  40.         con.close();//这是把连接还给连接池,而不是关闭连接  
  41.         rs.close();  
  42.         ps.close();  
  43.     }  

猜你喜欢

转载自www.cnblogs.com/albertzhangyu/p/8962941.html