• C3P0数据库连接池的简单使用


    2种方式

    1.导入c3p0的jar 

    1. //c3p0数据库连接池使用方法
    2. public static void main(String[] args) throws Exception {
    3. //创建数据库源对象
    4. ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    5. //通过配置文件mysql.properties 来获取数据库信息
    6. //创建properties对象
    7. Properties properties = new Properties();
    8. //读取mysql.properties文件 这边有异常我们直接抛出
    9. properties.load(new FileInputStream("src//mysql.properties"));
    10. //得到对应的key
    11. //用户名
    12. String user = properties.getProperty("user");
    13. //密码
    14. String password = properties.getProperty("password");
    15. //数据库连接地址
    16. String url = properties.getProperty("url");
    17. //数据库连接驱动
    18. String driver = properties.getProperty("driver");
    19. //给数据源comboPooledDataSource添加相关的参数
    20. //驱动
    21. comboPooledDataSource.setDriverClass(driver);
    22. //地址
    23. comboPooledDataSource.setJdbcUrl(url);
    24. //用户名
    25. comboPooledDataSource.setUser(user);
    26. //密码
    27. comboPooledDataSource.setPassword(password);
    28. //初始连接个数
    29. comboPooledDataSource.setInitialPoolSize(10);
    30. //最大连接个数
    31. comboPooledDataSource.setMaxPoolSize(50);
    32. //最小连接个数
    33. comboPooledDataSource.setMinPoolSize(5);
    34. //这个方法就是从DataSource接口实现的
    35. Connection connection = comboPooledDataSource.getConnection();
    36. //设置 增加的sql语句
    37. String sql = "insert into user value(?,?)";
    38. //得到preparedStatement对象
    39. PreparedStatement preparedStatement = connection.prepareStatement(sql);
    40. //给参数赋值 1就是第一个? admin就是给参数值
    41. preparedStatement.setString(1, "admin");
    42. preparedStatement.setString(2, "admin");
    43. //执行对数据库的操作
    44. int i = preparedStatement.executeUpdate();
    45. if (i > 0) {
    46. System.out.println("增加成功");
    47. }
    48. //关闭数据库连接池资源
    49. connection.close();
    50. preparedStatement.close();
    51. }

    1. user=用户名
    2. password=密码
    3. url=jdbc:mysql://localhost:3306/数据库名
    4. driver=com.mysql.jdbc.Driver

    第二种方式配置文件连接

    1. //第二种方式
    2. public void c3p0Test02() throws Exception {
    3. //创建数据库源对象 zx 就是xml文件的
    4. ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("name");
    5. //这个方法就是从DataSource接口实现的
    6. Connection connection = comboPooledDataSource.getConnection();
    7. //设置 增加的sql语句
    8. String sql = "insert into user value(?,?)";
    9. //得到preparedStatement对象
    10. PreparedStatement preparedStatement = connection.prepareStatement(sql);
    11. //给参数赋值 1就是第一个? admin就是给参数值
    12. preparedStatement.setString(1, "admin");
    13. preparedStatement.setString(2, "admin");
    14. //执行对数据库的操作
    15. int i = preparedStatement.executeUpdate();
    16. if (i > 0) {
    17. System.out.println("增加成功");
    18. }
    19. //关闭数据库连接池资源
    20. connection.close();
    21. preparedStatement.close();
    22. }

    配置文件信息

    1. "name">
    2. "driverClass">com.mysql.jdbc.Driver
    3. "jdbcUrl">jdbc:mysql://127.0.0.1:3306/数据库名称
    4. "user">用户名
    5. "password">密码
    6. "acquireIncrement">5
    7. "initialPoolSize">10
    8. "minPoolSize">5
    9. "maxPoolSize">10
    10. "maxStatements">5
    11. "maxStatementsPerConnection">2

    只是简单的使用 有不足的地方请大佬指点指点

  • 相关阅读:
    MySQL update正在执行中突然断电,数据是否更改成功?
    SpringCloud Alibaba微服务第6章之Gateway
    leetcode每天5题-Day42
    云原生爱好者周刊:使用树莓派组建 K8s 集群 | 2022-08-08
    【路径规划-TSP问题】基于粒子群结合蚁群算法求解旅行商问题附matlab代码
    k8s上对Pod的管理部分详解
    2022.7台式机装机指南(3060 + 12490F)
    科目三:右转弯
    Google Earth Engine ——影像无法正差加载可能是可视化参数设置得问题(visParams)
    C++学习笔记(面向对象部分开始6500字复习总结)
  • 原文地址:https://blog.csdn.net/qq_41740369/article/details/128137650