1. 作用
使用 Java 對數(shù)據(jù)庫進行增、刪、改、查操作
2. 環(huán)境
- commons-dbutils.jar
- c3p0.jar
- mchange-commons.jar
3. 使用
update()
- int update(String sql,Object… params)
- 此方法可以應(yīng)用于 INSERT、UPDATE、DELETE 操作
@Test
public void testUpdate() {
//1. 創(chuàng)建 QueryRunner 的實現(xiàn)類
QueryRunner queryRunner = new QueryRunner();
//2. 使用其 update 方法
// String sql = "DELETE FROM customers WHERE id IN(?)"; //刪除
// String sql = "INSERT INTO customers(name, email, birth) VALUES(?,?,?)"; //插入
String sql = "UPDATE customers SET name = ? WHERE id = ?";
Connection connection = null;
try {
connection = JDBCTools.getConnection();
queryRunner.update(connection, sql, "luw", 11);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, null, null);
}
}
query()
- 此方法取決于其
ResultSetHandler
參數(shù)的 handel 方法的返回值
class MyResultHandler implements ResultSetHandler{
@Override
public Object handle(ResultSet resultSet) throws SQLException {
List<Customer> customers = new ArrayList<>();
while(resultSet.next()){
Integer id = resultSet.getInt(1);
String name = resultSet.getString(2);
String email = resultSet.getString(3);
Date birth = resultSet.getDate(4);
Customer customer = new Customer(id, name, email, birth);
customers.add(customer);
}
return customers;
}
}
@Test
public void testQuery() {
QueryRunner queryRunner = new QueryRunner();
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT id, name, email, birth FROM customers";
Object obj = queryRunner.query(connection, sql, new MyResultHandler());
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, null, null);
}
}
ResultSetHandler 接口
- BeanHandler:用來把結(jié)果集的一條記錄(默認(rèn)為第一條記錄)轉(zhuǎn)為創(chuàng)建 BeanHandler 對象時傳入的參數(shù)對應(yīng)的對象,如下面的 Employee 對象
@Test
public void testBeanHandler(){
QueryRunner queryRunner = new QueryRunner();
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT id, name, email, birth FROM customers WHERE id = ?";
Customer customer = queryRunner.query(connection, sql, new BeanHandler<Customer>(Customer.class), 23);
System.out.println(customer);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, null, null);
}
}
- BeanListHandler:
- 把結(jié)果集轉(zhuǎn)為一個 List,該 List 中包含的是對象的集合(該 List 不為 null,但可
能為空集合,即 size() 方法返回 0) - 若 SQL 語句的確能夠查詢到記錄,List 中存放的對象為創(chuàng)建 BeanListHandler 時
傳入的對象
@Test
public void testBeanListHandler(){
QueryRunner queryRunner = new QueryRunner();
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT * FROM customers";
List<Customer> customers = queryRunner.query(connection, sql, new BeanListHandler<Customer>(Customer.class));
System.out.println(customers);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, null, null);
}
}
- MapHandler:返回 SQL 語句的第一條記錄對應(yīng)的 Map 對象
鍵:SQL 查詢的列名(不是列的別名) 值:列的值
@Test
public void testMapHandler(){
QueryRunner queryRunner = new QueryRunner();
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT * FROM customers";
Map<String, Object> customers = queryRunner.query(connection, sql, new MapHandler());
System.out.println(customers);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, null, null);
}
}
- MapListHandler:
- 將結(jié)果集轉(zhuǎn)為一個 Map 的 List,返回多條記錄的 Map 集合(List<Map<Object,Object>>)
- Map 對應(yīng)的查詢的一條記錄:鍵:SQL 查詢的列名(不是列的別名) 值:列的值
@Test
public void testMapListHandler(){
QueryRunner queryRunner = new QueryRunner();
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT * FROM customers";
List<Map<String, Object>> customers = queryRunner.query(connection, sql, new MapListHandler());
System.out.println(customers);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, null, null);
}
}
- ScalarHandler:把結(jié)果集轉(zhuǎn)為一個數(shù)值(可以是任意基本數(shù)據(jù)類型和字符串, Date 等)
@Test
public void testScalarHandler(){
QueryRunner queryRunner = new QueryRunner();
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT name FROM customers WHERE id = ?";
Object customers = queryRunner.query(connection, sql, new ScalarHandler<Customer>(), 2);
System.out.println(customers);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, null, null);
}
}