import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.BeanMapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import com.mchange.v2.c3p0.impl.NewPooledConnection;
/**
* 测试DBUtils类
*
*
* */
public class DBUtilsTest {
/**
* ScalarHandler为返回一个值,如果SQL语句中查询多列属性,默认只查询第一个列显示属性
*
* */
@Test
public void testScalarHandler(){
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql ="select name "
+ "from customers ";
Object name = queryRunner.query(connection, sql,
new ScalarHandler());
System.out.println(name);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}
/**
*
* MapHandler:将结果集装换为List
*
* **/
@Test
public void testMapHandler(){
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql ="select id,name,email,birth "
+ "from customers";
Map<String,Object> result = queryRunner.query(connection,
sql, new BeanMapHandler(Customer.class));
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}
/**
* BeanListHandler的应用
*
* */
@Test
public void testBeanListHandler(){
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql ="select id,name,email,birth "
+ "from customers";
List<Customer> customers = queryRunner.query(connection,
sql, new BeanListHandler(Customer.class));
System.out.println(customers);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}
/**
* BeanHandler可以在追加别名上应用
*
* */
@Test
public void testBeanHanlder(){
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql ="select id,name customerName,email,birth "
+ "from customers where id = ?";
Customer customer = queryRunner.query(connection,
sql, new BeanHandler(Customer.class),1);
System.out.println(customer);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}
QueryRunner queryRunner = new QueryRunner();
class MyResultSetHandler implements ResultSetHandler{
@Override
public Object handle(ResultSet resultSet) throws SQLException {
//System.out.println("handle......");
//return "hahahaha";
List<Customer> customers = new ArrayList<Customer>();
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(){
Connection connection = null;
try {
connection = JDBCTools.getConnection();
String sql ="select id,name,email,birth "
+ "from customers";
Object object = queryRunner.query(connection, sql,
new MyResultSetHandler());
System.out.println(object);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}
/**
* 测试DBUtils中QueryRunner类的update方法
* 该方法可用与insert、update、delete
* this class is thread safe.该类是线程安全的
* */
@Test
public void testQueryRunnerUpdate() {
//1.创建QueryRunner的实现类
QueryRunner queryRunner = new QueryRunner();
String sql = "DELETE FROM customers "
+ "where ID IN (?,?)";
Connection connection = null;
try {
connection = JDBCTools.getConnection();
//2使用QueryRunner的update方法进行更新
queryRunner.update(connection, sql, 1,1);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}
}