连接池(C3P0,DBCP,Druid)

分手后的思念是犯贱 2023-01-13 08:54 261阅读 0赞

目录

    • 一、C3P0连接池
    • 二、DBCP连接池
    • 三、Druid连接池

一、C3P0连接池

c3p0-config.xml(放在resources资源里)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <c3p0-config>
  3. <!--配置连接池mysql-->
  4. <named-config name="mysql">
  5. <property name="driverClass">com.mysql.jdbc.Driver</property>
  6. <property name="jdbcUrl">jdbc:mysql://localhost:3306/db1?characterEncoding=UTF-8</property>
  7. <property name="user">root</property>
  8. <property name="password">root</property>
  9. <property name="initialPoolSize">10</property>
  10. <property name="maxIdleTime">30</property>
  11. <property name="maxPoolSize">100</property>
  12. <property name="minPoolSize">10</property>
  13. </named-config>
  14. <!--配置连接池2,可以配置多个-->
  15. </c3p0-config>

C3P0工具类(放在utils包下)

  1. public class C3P0Utils {
  2. // //1.创建连接池对象
  3. //使用指定的配置
  4. public static ComboPooledDataSource dataSource=new ComboPooledDataSource("mysql");
  5. //获取连接的方法
  6. public static Connection getConnection() throws SQLException {
  7. return dataSource.getConnection();
  8. }
  9. //释放资源的方法
  10. public static void close(Connection connection, Statement statement) throws SQLException {
  11. if(connection!=null &&statement!=null){
  12. statement.close();
  13. statement.close();
  14. }
  15. }
  16. public static void close(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
  17. if(connection!=null &&statement!=null && resultSet!=null){
  18. resultSet.close();
  19. statement.close();
  20. statement.close();
  21. }
  22. }
  23. }

通过C3P0连接池向数据库插入数据

  1. public class Test01 {
  2. public static void main(String[] args) throws SQLException {
  3. //1.从连接池拿到对象
  4. Connection connection= DBCPUtils.getConnection();
  5. //2.获取statement对象
  6. String sql="insert into user values (?,?)";
  7. PreparedStatement preparedStatement = connection.prepareStatement(sql);
  8. //从用户那里拿到输入的语句
  9. Scanner s=new Scanner(System.in);
  10. System.out.println("请输入要插入的用户名");
  11. String name=s.next();
  12. System.out.println("请输入差的用户名的密码:");
  13. String password=s.next();
  14. // 设置参数
  15. preparedStatement.setString(1,name);
  16. preparedStatement.setString(2,password);
  17. //执行sql语句
  18. preparedStatement.executeUpdate();
  19. //关闭流操作
  20. DBCPUtils.close(connection,preparedStatement);
  21. }
  22. }

二、DBCP连接池

DBCP工具类

  1. public class DBCPUtils {
  2. //1. 定义常量
  3. public static final String DRIVERNAME="com.mysql.jdbc.Driver";
  4. public static final String URL="jdbc:mysql://localhost:3306/db1?characterEncoding=UTF-8";
  5. public static final String USER="root";
  6. public static final String PASSWORD="root";
  7. //2.创建连接池对象
  8. public static BasicDataSource dataSource=new BasicDataSource();
  9. //3.使用静态代码块进行配置
  10. static{
  11. dataSource.setDriverClassName(DRIVERNAME);
  12. dataSource.setUrl(URL);
  13. dataSource.setUsername(USER);
  14. dataSource.setPassword(PASSWORD);
  15. }
  16. //4.连接
  17. public static Connection getConnection() throws SQLException {
  18. Connection connection=dataSource.getConnection();
  19. return connection;
  20. }
  21. //5.关闭流
  22. public static void close(Connection connection, Statement statement) throws SQLException {
  23. if(connection!=null && statement!=null){
  24. statement.close();
  25. connection.close();
  26. }
  27. }
  28. public static void close(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
  29. if(connection!=null && statement!=null && resultSet!=null){
  30. resultSet.close();
  31. statement.close();
  32. connection.close();
  33. }
  34. }
  35. }

通过DBCP连接池登录数据库

  1. public class DBCPUtils {
  2. //1. 定义常量
  3. public static final String DRIVERNAME="com.mysql.jdbc.Driver";
  4. public static final String URL="jdbc:mysql://localhost:3306/db1?characterEncoding=UTF-8";
  5. public static final String USER="root";
  6. public static final String PASSWORD="root";
  7. //2.创建连接池对象
  8. public static BasicDataSource dataSource=new BasicDataSource();
  9. //3.使用静态代码块进行配置
  10. static{
  11. dataSource.setDriverClassName(DRIVERNAME);
  12. dataSource.setUrl(URL);
  13. dataSource.setUsername(USER);
  14. dataSource.setPassword(PASSWORD);
  15. }
  16. //4.连接
  17. public static Connection getConnection() throws SQLException {
  18. Connection connection=dataSource.getConnection();
  19. return connection;
  20. }
  21. //5.关闭流
  22. public static void close(Connection connection, Statement statement) throws SQLException {
  23. if(connection!=null && statement!=null){
  24. statement.close();
  25. connection.close();
  26. }
  27. }
  28. public static void close(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
  29. if(connection!=null && statement!=null && resultSet!=null){
  30. resultSet.close();
  31. statement.close();
  32. connection.close();
  33. }
  34. }
  35. }

三、Druid连接池

druid.properties(放在resources里面)

  1. driverClassName=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
  3. username=root
  4. password=root

Druid工具类

  1. public class DruidUtils {
  2. //定义成员变量
  3. public static DataSource dataSource;
  4. //静态代码块
  5. static{
  6. try {
  7. //3. 创建属性集对象
  8. Properties properties = new Properties();
  9. //4.加载配置文件 Druid 连接池不能够主动加载配置文件,需要指定文件
  10. InputStream inputStream=DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
  11. //5. Properties对象的Load方法 从字节流中读取配置信息
  12. properties.load(inputStream);
  13. //6. 通过工厂类获取连接池对象
  14. dataSource= DruidDataSourceFactory.createDataSource(properties);
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. //获取连接对象
  20. public static Connection getConnection(){
  21. try {
  22. return dataSource.getConnection();
  23. } catch (SQLException throwables) {
  24. throwables.printStackTrace();
  25. return null;
  26. }
  27. }
  28. //关闭连接方法
  29. public static void close(Connection connection, Statement statement) throws SQLException {
  30. if(connection!=null&&statement!=null){
  31. statement.close();
  32. connection.close();
  33. }
  34. }
  35. public static void close(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
  36. if(connection!=null&&statement!=null && resultSet!=null){
  37. resultSet.close();
  38. statement.close();
  39. connection.close();
  40. }
  41. }
  42. }

发表评论

表情:
评论列表 (有 0 条评论,261人围观)

还没有评论,来说两句吧...

相关阅读

    相关 c3p0连接

    c3p0连接池 什么是c3p0 c3p0是一个易于使用的库,通过使用jdbc3规范和jdbc2的可选扩展定义的功能来扩展传统JDBC驱动程序,从而使其“企业级可用”。

    相关 C3p0-数据库连接

    数据库链接池 数据库连接池的概念 数据库连数据库接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而再不是重新建立一个。 数据库链

    相关 JDBC连接-c3p0

        一个数据库连接对象对应着一个物理数据库连接,使用完后关闭连接。频繁的打开关闭连接会造成系统的性能低下,且可能造成数据库的缓存溢出,通常开发中使用连接池(严格来说叫做数据

    相关 C3P0连接详解

    1、概述      C3P0开源免费的连接池。目前使用它的开源项目由=有:Spring、Hibernate等。使用第三方工具需要导入jar包。C3P0使用时还需要添加配置