JDBC连接池&DBUtils

*JDBC连接池&DBUtils*

*今日内容介绍*

u *使用DBCP,C3P0连接池完成基本数据库的操作*

u *使用DBUtils完成CRUD的操作*

第1章 *使用连接池********重写工具类*

1.1 *连接池原理:*

理解为存放多个连接的集合。

在这里插入图片描述
目的:解决建立数据库连接耗费资源和时间很多的问题,提高性能。

1.2 *编写标准的数据源(规范)*

Java为数据库连接池提供了公共的接口:****javax.sql.DataSource****,各个厂商需要让自己的连接池实现这个接口。这样应用程序可以方便的切换不同厂商的连接池!

常见的连接池:DBCP、C3P0。

1.3 *常用的数据源配置*

1.3.1 *DBCP连接池*

DBCP:Apache推出的Database Connection Pool

使用步骤:

> 添加jar包 commons-dbcp-1.4.jar commons-pool-1.5.6.jar

> 添加属性资源文件

l 配置文件名称:*.properties

l 配置文件位置:建议src(classpath/类路径)

l 配置文件内容:properties不能编写中文

> 编写数据源工具类

在这里插入图片描述

1.3.2 *C3P0连接池*

C3P0开源免费的连接池!目前使用它的开源项目有:Spring、Hibernate等。使用第三方工具需要导入jar包,c3p0使用时还需要添加配置文件 c3p0-config.xml

使用步骤:

1、添加jar包

2、编写配置文件

c3p0-config.xml,放在src中(注:文件名一定不要写错)

在这里插入图片描述

3、编写工具类:

在这里插入图片描述

第1章 *使用DBUtils* *执行增删改查********的操作*

1.1 *案例分析*

简化JDBC代码开发,本案例我们将采用apache commons组件一个成员:DBUtils。

​ DBUtils就是JDBC的简化开发工具包。需要使用技术:连接池(获得连接),SQL语句都没有少。

1.2 *案例相关知识*

1.2.1 *JavaBean组件*

JavaBean就是一个类,在开发中常用于封装数据。具有如下特性

\1. 需要实现接口:java.io.Serializable ,通常偷懒省略了。

\2. 提供私有字段:private 类型 字段名;

\3. 提供getter/setter方法:

\4. 提供无参构造

public class Category {

​	

​	private String cid;

​	private String cname;

​	

​	public String getCid() {

​		return cid;

​	}

​	public void setCid(String cid) {

​		this.cid = cid;

​	}

​	public String getCname() {

​		return cname;

​	}

​	public void setCname(String cname) {

​		this.cname = cname;

​	}

  …toString….

}


1.3 *DBUtils完成CRUD*

1.3.1 *概述*

DBUtils是java编程中的数据库操作实用工具,小巧简单实用。

DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。

Dbutils三个核心功能介绍

l QueryRunner中提供对sql语句操作的API.

l ResultSetHandler接口,用于定义select操作后,怎样封装结果集.

l DbUtils类,它就是一个工具类,定义了关闭资源与事务处理的方法

1.3.2 *QueryRunner核心类*

l QueryRunner(DataSource ds) ,提供数据源(连接池),DBUtils底层自动维护连接connection

l update(String sql, Object… params) ,执行更新数据 insert update delete

l query(String sql, ResultSetHandler rsh, Object… params) ,执行查询 select

1.3.3 *ResultSetHandler结果集处理类*

ArrayHandler将结果集中的第一条记录封装到一个Object[]数组中,数组中的每一个元素就是这条记录中的每一个字段的值
ArrayListHandler将结果集中的每一条记录都封装到一个Object[]数组中,将这些数组在封装到List集合中。
BeanHandler将结果集中第一条记录封装到一个指定的javaBean中。
BeanListHandler将结果集中每一条记录封装到指定的javaBean中,将这些javaBean在封装到List集合中
ColumnListHandler将结果集中指定的列的字段值,封装到一个List集合中
MapHandler将结果集中第一条记录封装到了Map<String,Object>集合中,key就是字段名称,value就是字段值
MapListHandler将结果集中每一条记录封装到了Map<String,Object>集合中,key就是字段名称,value就是字段值,在将这些Map封装到List集合中。
ScalarHandler它是用于单个数据。例如select count(*) from 表操作。

1.3.4 *实现*

开发步骤:

1、创建项目,并导入jar包

2、创建连接池

3、编写测试类

1.3.4.1 *添加*
@Test

public void testInsert() {try {

​		

​		//1 核心类

​		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());//2 sql

​		String sql = "insert into category(cid,cname) values(?,?)";//3 参数

​		Object[] params = {"c006","家电"};//4执行

​		queryRunner.update(sql, params);

​		

​	} catch (Exception e) {throw new RuntimeException(e);} 

}
1.3.4.2 *更新*
@Test

public void testUpdate() throws Exception{try {

​		

​		//1 核心类

​		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());//2 sql

​		String sql = "update category set cname=? where cid = ?";//3 参数

​		Object[] params = {"家电","c006"};//4执行

​		queryRunner.update(sql, params);

​		

​	} catch (Exception e) {throw new RuntimeException(e);}}
1.3.4.3 *删除*
@Test

public void testDelete() throws Exception{try {

​		

​		//1 核心类

​		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());//2 sql

​		String sql = "delete from category where cid = ?";//3 参数

​		Object[] params = {"c006"};//4执行

​		queryRunner.update(sql, params);

​		

​	} catch (Exception e) {throw new RuntimeException(e);} 

}
1.3.4.4 *通过id查询*
@Test

public void testFindById() throws Exception{

​	

​	try {

​		

​		//1 核心类

​		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());//2 sql

​		String sql = "select * from category where cid=?";//3 参数

​		Object[] params = {"c003"};//4执行

​		Category category = queryRunner.query(sql, new BeanHandler<Category>(Category.class), params);

​		

​		System.out.println(category);} catch (Exception e) {throw new RuntimeException(e);} 

}
1.3.4.5 *查询所有*
@Test

public void testFindAll() throws Exception{

​	

​	try {

​		

​		//1 核心类

​		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());//2 sql

​		String sql = "select * from category";//3 参数

​		Object[] params = {};//4执行

​		List<Category> allCategory = queryRunner.query(sql, new BeanListHandler<Category>(Category.class), params);

​		

​		for (Category category : allCategory) {

​			System.out.println(category);}

​		

​	} catch (Exception e) {throw new RuntimeException(e);} 

}
1.3.4.6 *总记录数*
@Test

public void testCount() throws Exception{

​	

​	try {

​		

​		//1 核心类

​		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());//2 sql

​		String sql = "select count(*) from category";//3 参数

​		Object[] params = {};//4执行

​		Long numLong = (Long) queryRunner.query(sql, new ScalarHandler(), params);

​		

​		int num = numLong.intValue();

​		

​		System.out.println(num);} catch (Exception e) {throw new RuntimeException(e);} 

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值