DbUtils库是一套小巧的用来简化 JDBC 调用的库。JDBC源代码库单调且易出错,所以DBUtils 类抽象出所有简单的任务,让你更专注于使用JDBC做Query和Update的工作。
DbUtils的优势:
清爽整洁的代码。访问数据库的代码会大量的减少。剩下的代码更简介的表达了你的意图。
JavaBean属性自动从ResultSet中填充值。不用手动调用setXxx为属性赋值。
在使用JDBC访问数据库时,有大量重复的代码(注册驱动,获取连接,访问数据库,处理结果集,释放资源等)DbUtils抽象并封装了这些操作,方便开发人员DAO操作。
DBUtils简化了JDBC的开发步骤,使得我们可以用更少量的代码实现连接数据库的功能
Dbutils三个核心功能介绍
QueryRunner核心类:
jar包:
commons-dbutils-1.7.jar
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.30.jar
需要下载DBUtils的jar包:DBUtils下载地址
JavaBean类:
public class Account {
private Integer id;
private String name;
private Double money;
DBUtils自定义工具类
/**
* c3p0版本
* JDBC的工具类
* @author wanzhaocheng
*
*/
public class JDBCUtils2 {
//创建一个c3p0连接池:但是这个连接池只需要创建一次即可
private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
/**
* 获得连接的方法
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
/**
* 获得连接池
*/
public static DataSource getDataSource() {
return dataSource;
}
/**
* 释放资源的方法
* @return
*/
public static void release(Statement stmt,Connection conn) {
//释放资源
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void release(ResultSet rs, Statement stmt,Connection conn) {
//释放资源
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
/**
* DBUtils的增删改的操作
* @author wanzhaocheng
*/
public class DBUtilsDemo1 {
@Test
/**
* 添加操作
*/
public void demo1() throws SQLException {
//创建DBUtils核心类:QueryRunner:
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
queryRunner.update("insert into account values (null,?,?)","ddd",10000);
}
@Test
/**
* 修改操作
*/
public void demo2() throws SQLException {
//创建核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
queryRunner.update("update account set name=?,money=? where id =?","eee",20000,4);
}
@Test
/**
* 删除操作
*/
public void demo3() throws SQLException {
//创建核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
queryRunner.update("delete from account where id= ?",4);
}
}
/**
* DBUtils的查询操作
* @author wanzhaocheng
*
*/
public class DBUtilsDemo2 {
@Test
/**
* 查询一条记录
*/
public void demo1() throws SQLException{
//创建核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
//执行查询:
Account account = queryRunner.query("select * from account where id = ?", new ResultSetHandler<Account>() {
@Override
public Account handle(ResultSet rs) throws SQLException {
Account account = new Account();
while(rs.next()) {
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
}
return account;
}
}, 1);
System.out.println(account);
}
@Test
/**
* 查询多条记录
*/
public void demo2() throws SQLException{
//创建核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
//执行查询:
List<Account> list = queryRunner.query("select * from account", new ResultSetHandler<List<Account>>() {
@Override
public List<Account> handle(ResultSet rs) throws SQLException {
//创建一个集合,用于封装数据
List<Account> list = new ArrayList<Account>();
while(rs.next()) {
//将结果集中的数据封装到对象中
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
//将这个对象存入到list集合中
list.add(account);
}
return list;
}
});
//遍历集合,打印输出
for(Account account : list) {
System.out.println(account);
}
}
}
/**
* ResultSetHandler的实现类:
* ArrayHandler、ArrayListHandler
* BeanHandler、BeanListHandler
* MapHandler、MapListHandler
* ColumnListHandler、ScalarHandler、KeyedHandler
* @author wanzhaocheng
*/
public class DBUtilsDemo3 {
@Test
/**
* ArrayHandler:将一条数据封装到一个Object数组中
*/
public void demo1() throws SQLException {
// 创建DBUtils核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
Object[] objs = queryRunner.query("select * from account where id = ?", new ArrayHandler(), 1);
System.out.println(Arrays.toString(objs));
}
@Test
/**
* ArrayListHandler:将多条数据封装到一个装有Object数组的List集合中
*/
public void demo2() throws SQLException {
// 创建DBUtils核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
List<Object[]> list = queryRunner.query("select * from account", new ArrayListHandler());
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
}
@Test
/**
* BeanHandler:将一条记录封装到一个JavaBean中
*/
public void demo3() throws SQLException {
// 创建DBUtils核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
Account account = queryRunner.query("select * from account where id = ?",
new BeanHandler<Account>(Account.class), 2);
System.out.println(account.toString());
}
@Test
/**
* BeanListHandler:将多条数据封装到一个装有JavaBean的List集合中
*/
public void demo4() throws SQLException {
// 创建DBUtils核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
List<Account> list = queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class));
for (Account account : list) {
System.out.println(account.toString());
}
}
@Test
/**
* MapHandler:将一条记录封装到一个Map集合中,Map的key是列名,Map的value就是表中列的记录值
*/
public void demo5() throws SQLException {
// 创建DBUtils核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
Map<String, Object> map = queryRunner.query("select * from account where id = ?",new MapHandler(), 2);
System.out.println(map);
}
@Test
/**
* MapListHandler:将多条数据封装到一个装有Map的List集合中
*/
public void demo6() throws SQLException {
// 创建DBUtils核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
List<Map<String, Object>> list = queryRunner.query("select * from account", new MapListHandler());
for (Map map : list) {
System.out.println(map);
}
}
@Test
/**
* ColumnListHandler:将某列的值封装到List集合中
*/
public void demo7() throws SQLException {
// 创建DBUtils核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
List<Object> list = queryRunner.query("select name,money from account",new ColumnListHandler<Object>("name"));
for(Object object : list) {
System.out.println(object);
}
}
@Test
/**
* ScalarHandler:单值封装
*/
public void demo8() throws SQLException {
// 创建DBUtils核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
Object obj = queryRunner.query("select count(*) from account",new ScalarHandler());
System.out.println(obj);
}
@Test
/**
* KeyedHandler:将一条数据封装到一个Map集合中。将多条记录封装到一个装有Map集合的Map集合中,而且外面的Map的key是可以指定的
*/
public void demo9() throws SQLException {
// 创建DBUtils核心类
QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
Map<Object, Map<String, Object>> map = queryRunner.query("select * from account", new KeyedHandler<Object>("id"));
for (Object key : map.keySet()) {
System.out.println(key + "," + map.get(key));
}
}
}
代码下载:链接: https://pan.baidu.com/s/1tCgm8JETPrrv_7VSnIBTag
提取码: 2isr