基本测试

1、导包
c3p0
c3p0-0.9.2-pre5.jar
c3p0依赖
mchange-commons-java-0.2.3.jar
mysql驱动
mysql-connector-java-5.1.37-bin.jar

2、测试

  1. package cn.ant.Day18;
  2. import java.sql.Connection;
  3. import com.mchange.v2.c3p0.ComboPooledDataSource;
  4. public class C3p0Demo01 {
  5. public static void main(String[] args) throws Exception {
  6. // TODO Auto-generated method stub
  7. //1、创建连接池(获取数据源)
  8. ComboPooledDataSource dataSource=new ComboPooledDataSource();
  9. //2、基本参数设置
  10. dataSource.setDriverClass("com.mysql.jdbc.Driver");
  11. dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
  12. dataSource.setUser("root");
  13. dataSource.setPassword("123456");
  14. //3、设置其他参数
  15. dataSource.setInitialPoolSize(5);//初始的连接数
  16. dataSource.setMinPoolSize(3);//最小的连接
  17. dataSource.setMaxPoolSize(10);//最大的连接
  18. dataSource.setAcquireIncrement(2);//设置增长的连接数
  19. dataSource.setMaxIdleTime(30);//以秒为单位--最大空闲时间-当连接空闲了30秒就自动回收
  20. //4、从连接池中获取连接对象
  21. Connection conn=dataSource.getConnection();
  22. System.out.println(conn);
  23. }
  24. }

3、结果
image.png

配置文件使用c3p0

1、导包(3个)
2、编写配置文件

  1. <c3p0-config>
  2. <!-- 默认配置,如果没有指定则使用这个配置 只能有一个默认配置 -->
  3. <default-config>
  4. <property name="driverClass">com.mysql.jdbc.Driver</property>
  5. <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mydb</property>
  6. <property name="user">root</property>
  7. <property name="password">123456</property>
  8. <property name="checkoutTimeout">30000</property>
  9. <property name="idleConnectionTestPeriod">30</property>
  10. <property name="initialPoolSize">10</property>
  11. <property name="maxIdleTime">3</property>
  12. <property name="maxPoolSize">100</property>
  13. <property name="minPoolSize">10</property>
  14. <property name="maxStatements">200</property>
  15. <user-overrides user="test-user">
  16. <property name="maxPoolSize">10</property>
  17. <property name="minPoolSize">1</property>
  18. <property name="maxStatements">0</property>
  19. </user-overrides>
  20. </default-config>
  21. <!-- 命名的配置 可以写多个 -->
  22. <named-config name="ant">
  23. <!-- 连接数据库的4项基本参数 -->
  24. <property name="driverClass">com.mysql.jdbc.Driver</property>
  25. <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mydb</property>
  26. <property name="user">root</property>
  27. <property name="password">123456</property>
  28. <!-- 如果池中数据连接不够时一次增长多少个 -->
  29. <property name="acquireIncrement">5</property>
  30. <!-- 初始化连接数 -->
  31. <property name="initialPoolSize">20</property>
  32. <!-- 最小连接受 -->
  33. <property name="minPoolSize">10</property>
  34. <!-- 最大连接数 -->
  35. <property name="maxPoolSize">40</property>
  36. <!-- -JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量 -->
  37. <property name="maxStatements">0</property>
  38. <!-- 连接池内单个连接所拥有的最大缓存statements数 -->
  39. <property name="maxStatementsPerConnection">5</property>
  40. </named-config>
  41. </c3p0-config>

3、测试(使用默认配置default-config)

  1. package cn.ant.Day18;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import javax.sql.DataSource;
  5. import com.mchange.v2.c3p0.ComboPooledDataSource;
  6. public class C3P0Utils {
  7. private static ComboPooledDataSource dataSource=new ComboPooledDataSource();
  8. //封装一个方法获取连接对象
  9. public static Connection getConnection(){
  10. try {
  11. return dataSource.getConnection();
  12. } catch (SQLException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. return null;
  17. }
  18. //获取连接池对象 为了创建连接池的数据服务
  19. public static DataSource getSource(){
  20. return dataSource;
  21. }
  22. //测试
  23. public static void main(String[] args) {
  24. Connection conn=getConnection();
  25. System.out.println(conn);
  26. }
  27. }

image.png

4、测试,使用自定义的配置

  1. package cn.ant.Day18;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import javax.sql.DataSource;
  5. import com.mchange.v2.c3p0.ComboPooledDataSource;
  6. public class C3P0Utils {
  7. private static ComboPooledDataSource dataSource=new ComboPooledDataSource("ant");
  8. //封装一个方法获取连接对象
  9. public static Connection getConnection(){
  10. try {
  11. return dataSource.getConnection();
  12. } catch (SQLException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. return null;
  17. }
  18. //获取连接池对象 为了创建连接池的数据服务
  19. public static DataSource getSource(){
  20. return dataSource;
  21. }
  22. //测试
  23. public static void main(String[] args) {
  24. Connection conn=getConnection();
  25. System.out.println(conn);
  26. }
  27. }

image.png

注意

  • c3p0会自动加载c3p0配置文件,但是要求是c3p0的配置文件必须放在src目录下
  • 使用自定义配置的时候,需要给ComboPooledDataSource传递一个参数()中的ant,即名字