package Test;
import java.awt.geom.QuadCurve2D;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
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.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import com.mysql.jdbc.Connection;
public class testDBUTil {
QueryRunner queryrunner = new QueryRunner();
class MyResultSetHandle implements ResultSetHandler {
List<Customer> list = new ArrayList();
public Object handle(ResultSet rs) throws SQLException {
while (rs.next()) {
Integer id = rs.getInt(1);
String name = rs.getString(2);
String emil = rs.getString(3);
Customer customer = new Customer(id, name, emil);
list.add(customer);
}
return list;
}
}
@Test
//使用ScalarHandler:把结果集转化成一个数值返回。如果出现多条属性,默认返回第一条
public void testScalarHandler() throws Exception{
String user = "root";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/bank";
String driverclass = "com.mysql.jdbc.Driver";
Class.forName(driverclass);
String sql = "SELECT name from customers where id=2";
Connection con = (Connection) DriverManager.getConnection(url, user, password);
Object o=queryrunner.query(con, sql, new ScalarHandler());
System.out.println(o);
}
@Test
//用MapListHandler:将结果转化成一个list集合
//map对应的查询的一条记录 ,键:Sql查询的列名(不是列的别名),值:列的值
//而MapListHandler:则是返回多条记录对应的map集合
public void testMapListHandler() throws Exception{
String user = "root";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/bank";
String driverclass = "com.mysql.jdbc.Driver";
Class.forName(driverclass);
String sql = "SELECT *from customers ";
Connection con = (Connection) DriverManager.getConnection(url, user, password);
List<Map<String, Object>> list=queryrunner.query(con, sql,new MapListHandler());
System.out.println(list);
}
@Test
//使用MapHandler:此时返回的是sql对应的第一条记录对应的Map对象
//键:Sql查询的列名(不是列的别名),值:列的值
public void testMapHandler() throws Exception{
String user = "root";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/bank";
String driverclass = "com.mysql.jdbc.Driver";
Class.forName(driverclass);
String sql = "SELECT *from customers ";
Connection con = (Connection) DriverManager.getConnection(url, user, password);
Map<String,Object> result=queryrunner.query(con, sql,new MapHandler());
System.out.println(result);
}
@Test
//使用BeanListHandler,将结果集转化成一个List集合,该List不为null,但可能为空,若SQL语句的确能够查到记录,List中存放
//BeanListHandler传入的class对象对应的对象。
public void testBeanListHandle() throws Exception {
String user = "root";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/bank";
String driverclass = "com.mysql.jdbc.Driver";
Class.forName(driverclass);
String sql = "SELECT *from customers ";
Connection con = (Connection) DriverManager.getConnection(url, user, password);
System.out.println(con);
List<Customer> customers = (List<Customer>) queryrunner.query(con, sql, new BeanListHandler(Customer.class));
System.out.println(customers);
}
@Test
// BeanHandler查询,把结果集的第一条记录转为创建BeanHandle对象时传入的class参数对应的对象。
public void testBeanHandle() throws Exception {
List<Customer> list1 = new ArrayList();
String user = "root";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/bank";
String driverclass = "com.mysql.jdbc.Driver";
Class.forName(driverclass);
String sql = "SELECT * from customers where id=(?)";
Connection con = (Connection) DriverManager.getConnection(url, user, password);
//用BeanHandle处理多条数据的时候只返回第一条数据。
Customer customer = (Customer) queryrunner.query(con, sql, new BeanHandler(Customer.class), 1);
System.out.println(customer);
}
@Test // 查询操作
public void check() throws Exception {
List<Customer> list1 = new ArrayList();
String user = "root";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/bank";
String driverclass = "com.mysql.jdbc.Driver";
Class.forName(driverclass);
Connection con = (Connection) DriverManager.getConnection(url, user, password);
String str = "SELECT id,name,emial FROM CUSTOMERS";
list1 = (List<Customer>) queryrunner.query(con, str, new MyResultSetHandle());
System.out.println(list1);
}
@Test // 更新操作
public void test() throws Exception {
String user = "root";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/bank";
String driverclass = "com.mysql.jdbc.Driver";
Class.forName(driverclass);
Connection con = (Connection) DriverManager.getConnection(url, user, password);
String sql = "DELETE FROM banking where id IN(?,?,?)";
queryrunner.update(con, sql, 8, 12, 15);
}
}