commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习a成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能。
QueryRunner类
该类简单化了SQL查询,它与ResultSetHandler组合在一起使用可以完成大部分的数据库操作,能够大大减少编码量。
QueryRunner类提供了两个构造器:
QueryRunner类的主要方法:
ResultSetHandler接口及实现类
该接口用于处理 java.sql.ResultSet,将数据按要求转换为另一种形式。
ResultSetHandler 接口提供了一个单独的方法:Object handle (java.sql.ResultSet .rs)。
接口的主要实现类:
步骤一:
导入DBUtils.jar和Druid.jar包,并build path 到当前工程下
步骤二:
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver
initialSize=10
maxActive=20
maxWait=1000
filters=wall
package com.atguigu3.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbutils.DbUtils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class JDBCutils {
//使用Druid德鲁伊数据连接池技术
private static DataSource source;
static {
try {
Properties pros = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
pros.load(is);
source = DruidDataSourceFactory.createDataSource(pros);
} catch (Exception e) {
e.printStackTrace();
}
}
获取数据库的连接
public static Connection getDruidConnection() throws SQLException {
Connection conn = source.getConnection();
return conn;
}
使用dbutils.jar中提供的DBUtils工具类,实现资源的关闭
public static void close(Connection conn,Statement ps,ResultSet rs) {
DbUtils.closeQuietly(conn);
DbUtils.closeQuietly(ps);
DbUtils.closeQuietly(rs);
}
}
步骤三:
DBUtils 实现CRUD操作
QueryRunnerTest
package com.atguigu3.util;
import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
public class QueryRunnerTest {
测试插入 增删改操作一致
@Test
public void testInsert() {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCutils.getDruidConnection();
String sql = "insert into customers (name,email,birth) values(?,?,?)";
int count = runner.update(conn,sql,"dh","[email protected]","1997-10-21");
System.out.println("添加了"+count+"条记录");
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCutils.close(conn, null,null);
}
}
测试查询操作
BeanHandler:是ResultSetHandler接口的实现类,用于封装表中的一条记录
@Test
public void testQuery1() {
Connection conn =null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCutils.getDruidConnection();
String sql = "select id,name,email,birth from customers where id = ?";
BeanHandler<Customer> handler = new BeanHandler<>(Customer.class);
Customer customer = runner.query(conn,sql, handler,3);
System.out.println(customer);
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCutils.close(conn, null,null);
}
}
测试查询多条记录
BeanListHandler:是ResultSetHandler接口的实现类,用于封装表中的多条记录,构成的集合
@Test
public void testQuery2() {
Connection conn =null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCutils.getDruidConnection();
String sql = "select id,name,email,birth from customers where id < ?";
BeanListHandler<Customer> handler =new BeanListHandler<>(Customer.class);
List<Customer> list = runner.query(conn,sql, handler,10);
list.forEach(System.out::println);
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCutils.close(conn, null,null);
}
}
MapHandler:是ResultSetHandler接口的实现类 对应表中的一条记录
将字段及相应字段的值作为map中的key和value
@Test
public void testQuery3() {
Connection conn =null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCutils.getDruidConnection();
String sql = "select id,name,email,birth from customers where id = ?";
MapHandler handler = new MapHandler();
Map<String, Object> map = runner.query(conn,sql, handler,10);
System.out.println(map);
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCutils.close(conn, null,null);
}
}
MapListHandler:是ResultSetHandler接口的实现类 对应表中的多条记录
list中存储多个map 将字段及相应字段的值作为map中的key和value
@Test
public void testQuery4() {
Connection conn =null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCutils.getDruidConnection();
String sql = "select id,name,email,birth from customers where id < ?";
MapListHandler handler = new MapListHandler();
List<Map<String, Object>> list = runner.query(conn,sql, handler,10);
list.forEach(System.out::println);
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCutils.close(conn, null,null);
}
}
ScalarHandler:是ResultSetHandler接口的实现类
用于查询特殊值 例如count、max、min、avg等函数的返回值
@Test
public void testQuery5() {
Connection conn =null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCutils.getDruidConnection();
String sql = "select count(*) from customers";
ScalarHandler handler = new ScalarHandler();
Long count =(Long) runner.query(conn,sql, handler);
System.out.println(count);
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCutils.close(conn, null,null);
}
}
@Test
public void testQuery6() {
Connection conn =null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCutils.getDruidConnection();
String sql = "select max(birth) from customers";
ScalarHandler handler = new ScalarHandler();
Date maxbirth=(Date) runner.query(conn,sql, handler);
System.out.println(maxbirth);
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCutils.close(conn, null,null);
}
}
}
以上步骤,可以实现对数据库增删改查的基本操作。