经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MySQL » 查看文章
数据连接池
来源:cnblogs  作者:是雾终会散  时间:2022/1/17 11:01:08  对本文有异议

dbcp

1.引入jar包

 

 

 导入这两个jar包

下载jar包地址:Maven Repository: Search/Browse/Explore (mvnrepository.com)

(进去网站后直接在搜索框搜索并下载即可)

2.配置后缀为 .properties  文件

如例:

  1. 1 #<!-- 连接设置 -->
  2. 2 driverClassName=com.mysql.cj.jdbc.Driver
  3. 3 url=jdbc:mysql://localhost:3306/test02
  4. 4 username=root
  5. 5 password=root
  6. 6
  7. 7 #<!-- 初始化连接 -->
  8. 8 initialSize=10
  9. 9
  10. 10 #<!-- 最大连接数量 -->
  11. 11 maxActive=50
  12. 12
  13. 13 #<!-- 最大空闲连接 -->
  14. 14 maxIdle=20
  15. 15
  16. 16 #<!-- 最小空闲连接 -->
  17. 17 minIdle=5
  18. 18
  19. 19 #<!-- 超时等待时间(单位毫秒) -->
  20. 20 maxWait=50000
  21. 21
  22. 22 #<!-- 编码方式 -->
  23. 23 connectionProperties=useUnicode=true;characterEncoding=utf8
  24. 24
  25. 25 ##<!-- 指定由连接池所创建的连接自动提交 -->
  26. 26 defaultAutoCommit=true
  27. 27
  28. 28 #<!-- 指定由连接池所创建的连接的事务级别 -->
  29. 29 defaultTransactionIsolation=REPEATABLE_READ

3.创建utils包(获取connection,释放连接资源)

  1. 1 public class JdbcUtils_c3p0 {
  2. 2
  3. 3 private static DataSource dataSource;
  4. 4 static {
  5. 5 try {
  6. 6
  7. 7 //创建数据源 工厂模式 --》创建
  8. 8 dataSource = new ComboPooledDataSource();
  9. 9
  10. 10
  11. 11 } catch (Exception e) {
  12. 12 e.printStackTrace();
  13. 13 }
  14. 14 }
  15. 15
  16. 16 //获取连接
  17. 17 public static Connection getConnection() throws Exception{
  18. 18 return dataSource.getConnection();
  19. 19
  20. 20 }
  21. 21 //释放链接资源
  22. 22 public static void release(Connection connection, Statement statement, ResultSet resultSet){
  23. 23 try {
  24. 24 if (resultSet != null) {
  25. 25 resultSet.close();
  26. 26 }
  27. 27 if (statement != null) {
  28. 28 statement.close();
  29. 29 }
  30. 30 if (connection != null) {
  31. 31 connection.close();
  32. 32 }
  33. 33 }catch (Exception e){
  34. 34 e.printStackTrace();
  35. 35 }
  36. 36 }
  37. 37 }

 

4.测试

(我这里是以添加一条数据为例)

  1. 1 Connection connection = null;
  2. 2 PreparedStatement ps = null;
  3. 3 ResultSet resultSet =null;
  4. 4 //1.获取数据库连接
  5. 5 try {
  6. 6 connection = JdbcUtils_c3p0.getConnection();
  7. 7 String sql = "insert into student (name,sex,birthday,tall) values (?,?,?,?)";
  8. 8 ps = connection.prepareStatement(sql); //预编译sql 先写sql,然后不执行
  9. 9 //手动为参数赋值
  10. 10 ps.setString(1, "嗷嗷嗷");
  11. 11 ps.setString(2, "男");
  12. 12 ps.setString(3, "2002.1.8");
  13. 13 ps.setDouble(4, 1.75);
  14. 14 //执行
  15. 15 ps.executeUpdate();
  16. 16
  17. 17 } catch (Exception e) {
  18. 18 e.printStackTrace();
  19. 19 } finally {
  20. 20 JdbcUtils.release(connection, ps, resultSet);
  21. 21 }
  22. 22 }

 

原文链接:http://www.cnblogs.com/qiaoyuqi/p/15780855.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号