0x01 介绍

C3P0是一个开源的JDBC连接池,它实现了数据源与JNDI绑定,支持JDBC3规范和实现了JDBC2的标准扩展说明的Connection和Statement池的DataSources对象

即将用于连接数据库的连接整合在一起形成一个随取随用的数据库连接池(Connection pool)

0x02 环境搭配

如果还不会搭建jsp环境的可以按照下面的文章跟着搭建

Mac版IDEA创建maven web项目-详细过程: https://www.yuque.com/pmiaowu/gpy1q8/npv0fr
JSP Servlet实例: https://www.yuque.com/pmiaowu/gpy1q8/egyodf

0x03 导入相关的包

按照上面的搭建完环境以后,就找到pom.xml找到<dependencies>标签
然后如下使用maven自动添加包

  1. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>5.1.41</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
  8. <dependency>
  9. <groupId>com.mchange</groupId>
  10. <artifactId>c3p0</artifactId>
  11. <version>0.9.2</version>
  12. </dependency>

image.png

0x04 C3P0连接池

  1. # 目录结构
  2. ├── src
  3. └── main
  4. └── webapp
  5. └── com
  6. ├── Filter
  7. └── ...
  8. └── Servlet
  9. ├── ...
  10. ├── C3P0Demo1.java
  11. ├── C3P0Demo2.java
  12. └── C3P0Demo3.java
  13. └── WEB-INF
  14. └── web.xml
  15. └── index.jsp
  16. └── c3p0-config.xml

我这里就连接MySQL数据库进行演示了

0x04.1 没有配置文件的使用方法

Servlet目录创建C3P0Demo1.java

  1. // 文件名字: C3P0Demo1.java
  2. package com.Servlet;
  3. import com.mchange.v2.c3p0.ComboPooledDataSource;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.beans.PropertyVetoException;
  9. import java.io.IOException;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. @WebServlet("/C3P0Demo1")
  15. public class C3P0Demo1 extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  18. String DRIVER_NAME = "com.mysql.jdbc.Driver";
  19. String URL = "jdbc:mysql://192.168.24.145:3306/mysql";
  20. String USER_NAME = "root";
  21. String PASSWORD = "123456";
  22. Connection conn = null;
  23. PreparedStatement ps = null;
  24. ComboPooledDataSource dataSource = new ComboPooledDataSource();
  25. try {
  26. dataSource.setDriverClass(DRIVER_NAME);
  27. dataSource.setJdbcUrl(URL);
  28. dataSource.setUser(USER_NAME);
  29. dataSource.setPassword(PASSWORD);
  30. conn = dataSource.getConnection();
  31. String sql = "SELECT * FROM `user`";
  32. ps = conn.prepareStatement(sql);
  33. ResultSet rs = ps.executeQuery();
  34. response.getWriter().println(" ");
  35. response.getWriter().println("C3P0Demo1.java");
  36. while (rs.next()) {
  37. response.getWriter().println(" ");
  38. response.getWriter().println("用户名:" + rs.getString("User") + " " + "密码:" + rs.getString("Password"));
  39. }
  40. } catch (PropertyVetoException | SQLException e) {
  41. e.printStackTrace();
  42. } finally {
  43. //释放stmt
  44. if (ps != null) {
  45. try {
  46. ps.close();
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. if (conn != null) {
  52. try {
  53. conn.close();
  54. } catch (SQLException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. }
  60. }
  61. // 执行该文件
  62. // 例如: http://127.0.0.1:8081/mavenJspTest_war/C3P0Demo1

image.png

0x04.2 有配置文件的使用方法一

默认配置法,适用于单一的数据库连接使用

src这个目录下的同级,记住一定是要在src这个目录下,创建一个c3p0-config.xml

  1. // c3p0-config.xml的内容
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <c3p0-config>
  4. <default-config>
  5. <property name="driverClass">com.mysql.jdbc.Driver</property>
  6. <property name="jdbcUrl">jdbc:mysql://192.168.24.145:3306/mysql</property>
  7. <property name="user">root</property>
  8. <property name="password">123456</property>
  9. </default-config>
  10. </c3p0-config>

然后在Servlet目录创建C3P0Demo2.java

  1. // 文件名字: C3P0Demo2.java
  2. package com.Servlet;
  3. import com.mchange.v2.c3p0.ComboPooledDataSource;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.sql.Connection;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. @WebServlet("/C3P0Demo2")
  14. public class C3P0Demo2 extends HttpServlet {
  15. @Override
  16. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  17. Connection conn = null;
  18. PreparedStatement ps = null;
  19. ComboPooledDataSource dataSource = new ComboPooledDataSource();
  20. try {
  21. conn = dataSource.getConnection();
  22. String sql = "SELECT * FROM `user`";
  23. ps = conn.prepareStatement(sql);
  24. ResultSet rs = ps.executeQuery();
  25. response.getWriter().println(" ");
  26. response.getWriter().println("C3P0Demo2.java");
  27. while (rs.next()) {
  28. response.getWriter().println(" ");
  29. response.getWriter().println("用户名:" + rs.getString("User") + " " + "密码:" + rs.getString("Password"));
  30. }
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. } finally {
  34. //释放stmt
  35. if (ps != null) {
  36. try {
  37. ps.close();
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. if (conn != null) {
  43. try {
  44. conn.close();
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50. }
  51. }
  52. // 执行该文件
  53. // 例如: http://127.0.0.1:8081/mavenJspTest_war/C3P0Demo2

image.png

0x04.3 有配置文件的使用方法二

可能我们需要连接多个数据库那么这个时候,该方法就可以使用上了
例如我们新增加一个名字叫c3p0-mysql-test的连接池

src这个目录下的同级,记住一定是要在src这个目录下,创建一个c3p0-config.xml

  1. // c3p0-config.xml的内容
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <c3p0-config>
  4. <named-config name="c3p0-mysql-test">
  5. <property name="driverClass">com.mysql.jdbc.Driver</property>
  6. <property name="jdbcUrl">jdbc:mysql://192.168.24.145:3306/mysql</property>
  7. <property name="user">root</property>
  8. <property name="password">123456</property>
  9. </named-config>
  10. </c3p0-config>

image.png

然后在Servlet目录创建C3P0Demo3.java

  1. // 文件名字: C3P0Demo3.java
  2. // 主要是ComboPooledDataSource里面带连接池名字即可
  3. package com.Servlet;
  4. import com.mchange.v2.c3p0.ComboPooledDataSource;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. @WebServlet("/C3P0Demo3")
  15. public class C3P0Demo3 extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  18. Connection conn = null;
  19. PreparedStatement ps = null;
  20. ComboPooledDataSource dataSource = new ComboPooledDataSource("c3p0-mysql-test");
  21. try {
  22. conn = dataSource.getConnection();
  23. String sql = "SELECT * FROM `user`";
  24. ps = conn.prepareStatement(sql);
  25. ResultSet rs = ps.executeQuery();
  26. response.getWriter().println(" ");
  27. response.getWriter().println("C3P0Demo3.java");
  28. while (rs.next()) {
  29. response.getWriter().println(" ");
  30. response.getWriter().println("用户名:" + rs.getString("User") + " " + "密码:" + rs.getString("Password"));
  31. }
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. } finally {
  35. //释放stmt
  36. if (ps != null) {
  37. try {
  38. ps.close();
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. if (conn != null) {
  44. try {
  45. conn.close();
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51. }
  52. }
  53. // 执行该文件
  54. // 例如: http://127.0.0.1:8081/mavenJspTest_war/C3P0Demo3

image.png

0x05 小结

在实战中,如果对方使用了c3p0作为数据源
那么知道去src这个目录下,查找c3p0-config.xml获取数据库账号密码即可