##概述

DBCP的全称是:DataBase connection pool,翻译是:数据库连接池。

在Java操作数据库方式一JDBC使用详解中说到直接使用JDBC非常消耗资源。为了避免频繁关闭链接数据库,所以出现了DBCP。

DBCP的工作原理是:首先通过连接池预先同数据库建立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中取出,用完后再放回去,从而避免繁关闭链接数据库,减少资源的消耗。

DBCP是 apache 的一个开源工具。

##准备工作

在使用JDBC连接数据库之前,首先要有数据库,数据库要创建表。我的数据库信息如下:

  1. 数据库类型:MySql。
  2. 数据库名字:xia。
  3. 用户名:root。
  4. 密码:root.
  5. 创建数据库表student。
create table student(id int primary key auto_increment,name varchar(20),age int
);

##开发环境

  1. 操作系统:MACOS。
  2. 开发工具:IntelliJ IDEA。
  3. Java版本:jdk1.8。
  4. 使用maven管理jar包。

##正式开发

一,在pom.xml文件中引入需要jar的依赖

        <!--mysql驱动,由于DBCP封装的JDBC,所以仍然需要mysql驱动包--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>6.0.6</version></dependency><!--<!– dbcp包 –>--><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils --><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.4</version></dependency>

二,得到DataSource对象

public class DBCPUtils {private static BasicDataSource dataSource;static {dataSource = new BasicDataSource();//基本设置dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/xia");dataSource.setUsername("root");dataSource.setPassword("root");//高级设置dataSource.setInitialSize(10);//初始化连接dataSource.setMinIdle(5);//最小空闲连接dataSource.setMaxIdle(20);//最大空闲连接dataSource.setMaxActive(50);//最大连接数量dataSource.setMaxWait(1000);//超时等待时间以毫秒为单位}/*** 获取DataSource对象* @return*/public static DataSource getDataSource() {return dataSource;}
}

三,插入操作

public static boolean insertStudent(Student student) {try {//1,得到dataSource对象,DataSource dataSource = DBCPUtils.getDataSource();//2,得到QueryRunner对象QueryRunner queryRunner = new QueryRunner(dataSource);//3,执行插入操作sqlqueryRunner.update("insert into student (name,age) values(?,?)", student.getName(), student.getAge());return true;} catch (Exception e) {e.printStackTrace();return false;}}

四,根据id查找单个对象

public static Student selectStudent(int id) {try {//1,得到dataSource对象,DataSource dataSource = DBCPUtils.getDataSource();//2,得到QueryRunner对象QueryRunner queryRunner = new QueryRunner(dataSource);//3,执行查询作sqlStudent student = queryRunner.query("select id,name,age from student where id=?", new BeanHandler<>(Student.class), id);return student;} catch (Exception e) {e.printStackTrace();return null;}}

五,根据其他条件得到对象集合

public static List<Student> selectUserList(int age) {try {//1,得到dataSource对象,DataSource dataSource = DBCPUtils.getDataSource();//2,得到QueryRunner对象QueryRunner queryRunner = new QueryRunner(dataSource);//3,执行查询作sqlList<Student> studentList = queryRunner.query("select id,name,age from student where age=?", new BeanListHandler<>(Student.class), age);return studentList;} catch (Exception e) {e.printStackTrace();return null;}}

六,更新操作

public static boolean updateStudent(Student student) {try {//1,得到dataSource对象,DataSource dataSource = DBCPUtils.getDataSource();//2,得到QueryRunner对象QueryRunner queryRunner = new QueryRunner(dataSource);//3,执行更新操作sqlqueryRunner.update("update student set age = ? where id = ?", student.getAge(), student.getId());return true;} catch (Exception e) {e.printStackTrace();return false;}}

七,删除操作

public static boolean deleteStudent(int id) {try {//1,得到dataSource对象,DataSource dataSource = DBCPUtils.getDataSource();//2,得到QueryRunner对象QueryRunner queryRunner = new QueryRunner(dataSource);//3,执行删除操作sqlqueryRunner.update("delete from student where id = ?", id);return true;} catch (Exception e) {e.printStackTrace();return false;}}

##使用配置文件配置数据库参数

在实际开发中我们通常创建一个properties配置文件,然后把数据库参数都放在配置文件中,这样便于数据库参数的管理,当数据库参数变化时可以快速找到配置文件然后进行修改。

在resource目录下创建dbcpconfig.properties文件,文件目录位置如下:

文件内容如下:

#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/xia
username=root
password=root#初始化连接
initialSize=10
#最大连接数量
maxActive=50
#最大空闲连接
maxIdle=20
#最小空闲连接
minIdle=5

此时得到DataSource对象的代码如下:

public class DBCPUtils {private static DataSource dataSource;static {try {InputStream in = DBCPUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");Properties props = new Properties();props.load(in);dataSource = BasicDataSourceFactory.createDataSource(props);} catch (Exception e) {e.printStackTrace();throw new ExceptionInInitializerError(e);}}/*** 获取DataSource对象* @return*/public static DataSource getDataSource() {return dataSource;}
}

其他增删改成操作的代码依旧与上面同。

##总结

一,使用DBCP的重点是获取DataSource对象,然后再创建QueryRunner对象,然后就可以进行增删改查操作。

二,使用DBCP不仅可以节约资源,而且可以直接将查询结果封装成对象,方便使用。

三,在实际开发中建议使用properties配置文件。

Java操作数据库方式二DBCP使用详解相关推荐

  1. Java操作数据库方式(六)DataSource详解

    ##概述 在java世界里操作数据库有很多方式,在众多方式中除了JDBC外都有DataSource对象. DataSource可以看作数据源,它封装了数据库参数,连接数据库,程序中操作DataSour ...

  2. Java操作数据库方式五MyBatis使用入门

    ##概述 ##MyBatis是什么 MyBatis是一个持久层框架,作用是在java项目中操作数据库. ##MyBatis介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年 ...

  3. java配置文件实现方式_java相关:详解Spring加载Properties配置文件的四种方式

    java相关:详解Spring加载Properties配置文件的四种方式 发布于 2020-4-29| 复制链接 摘记: 一.通过 context:property-placeholder 标签实现配 ...

  4. 一文快速回顾 Java 操作数据库的方式-JDBC

    前言 数据库的重要性不言而喻,不管是什么系统,什么应用软件,也不管它们是 Windows 上的应用程序,还是 Web 应用程序,存储(持久化)和查询(检索)数据都是核心的功能. 大家学习数据库时,比如 ...

  5. 理解Java操作数据库原理

    2019独角兽企业重金招聘Python工程师标准>>> 参考:https://blog.csdn.net/xiaozhegaa/article/details/70208646 上面 ...

  6. 【职坐标】java面向对象三大特性(二)——继承详解

    [职坐标]java面向对象三大特性(二)--继承详解 a) 什么是继承? i. 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可 b) ...

  7. python怎么安装myqr模块-python二维码操作:对QRCode和MyQR入门详解

    python是所有编程语言中模块最丰富的 生活中常见的二维码功能在使用python第三方库来生成十分容易 三个大矩形是定位图案,用于标记二维码的大小.这三个定位图案有白边,通过这三个矩形就可以标识一个 ...

  8. c#操作数据库(二)dataAdapter篇

    c#操作数据库(二)dataAdapter篇 2009-04-01 21:29 受够了ctrl+c加ctrl+v,忍无可忍之第二篇. 上篇博客介绍了用datareader操作数据库,dataReade ...

  9. python怎么安装myqr_python二维码操作:对QRCode和MyQR入门详解

    python是所有编程语言中模块最丰富的 生活中常见的二维码功能在使用python第三方库来生成十分容易 三个大矩形是定位图案,用于标记二维码的大小.这三个定位图案有白边,通过这三个矩形就可以标识一个 ...

最新文章

  1. 4G EPS 中的无线资源类型
  2. JAVA并发编程8_线程池的使用
  3. AnjularJS笔记5--ng-repeat跟据ID判断重复性
  4. 会议 | 2019 全国知识图谱与语义大会 (CCKS 2019)
  5. [ SHELL编程 ] 远程服务器传输文件
  6. 自然语言3——官网介绍
  7. 用ssh从ubuntu系统向ubuntu系统服务器发送文件
  8. ServletContext的应用(共享数据、获取初始化参数、请求转发、读取资源文件)【源码解析】
  9. 软件需求分析教程阅读笔记二
  10. 值得收藏的130个神器网站
  11. 网站地图在线生成html,sitemap_网站地图_站点地图_在线生成_专注在线服务工具开发与同步部署 - sitemap 生成器...
  12. 【radon变换原理讲解及利用python库函数快速实现】
  13. windows 系统 system 进程占用80端口
  14. 【初/中级前端面经】中小型公司面试时都会问些什么?
  15. qcloud.login 登录失败,可能是网络错误或者服务器发生异常的多种解决方法
  16. 咪咕盒子链接服务器失败_咪咕打卡正式开启,你的疑问都可以在这里找答案!...
  17. spark 无法读取hive 3.x的表数据
  18. I2C-两线外设接口-用于ArduinoNano, uno ,Mega2560
  19. 电灯图画门讲台计算机用英语怎么读,新版pep四年级英语上册单词练习(汉译英)...
  20. 巧借“中国制造2025”东风占领“智”高点

热门文章

  1. java给图片加水印_java中怎么样将水印加在图片的上面或者下面
  2. 如何去使用Python分析股票数据?学到就是赚到
  3. Kotlin Android Extensions使用指南
  4. 解决IBM Security AppScan扫描出现检测到RC4密码套件问题
  5. Anylogic各个版本的功能对比
  6. 33岁的外行妈妈,转行金融业可行吗?(此贴仅限于个人感悟)
  7. 有什么软件可以测试面膜的好坏,检验面膜好坏的小妙招
  8. 项目管理文档_免费直播 | 用项目管理“武装”质量管理体系
  9. 项目管理文档_免费直播 | 项目管理模式“武装”质量管理体系
  10. 图像处理特征不变算子系列之Moravec算子(一)