apache-dbutils

DbUtils

DbUtils:都是静态方法,一系列的close()方法;

  • QueryRunner类:

  • update():执行insert、update、delete;

  • query():执行select语句;

  • batch():执行批处理。

连接

/*
 * QueryRunner
 *  构造方法:
 *   public QueryRunner();
 *   public QueryRunner(DataSource);  //有参构造需要传入连接池
 *  成员方法 :
 *  update(String sql, Object... params) 
 *  query(String sql, ResultSetHandler<T> rsh, Object... params) 
 */

/*
 * DBUtil提供的工具类:
 *  query(String sql, ResultSetHandler<T> rsh, Object... params) :select
 *  
 *  操作一行
 *  	ArrayHandler      : 数组   Obejct[]
 *      BeanHandler       : 对象   Object   !!!!!!!!!!!!!!!
 *      MapHandler        : Map集合   Map<String,Object>
 *  操作所有行
 *      ArrayListHandler: List集合   List<Obejct[]>
 *      BeanListHandler : List集合   List<Object> !!!!!!!!!!!!!!!!!!
 *      MapListHandler  : List集合   List<Map<String,Object>>
 *  操作一列
 *     ColumnListHandler: List集合 List<Object>
 *  操作单值
 *    ScalarHandler  :    Object对象  Object
 */
	
	//1:ArrayHandler:
	//负责将结果集中第一行的数据放入数组
	@Test
	public void func1() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		//	Object[] array = {1,"联想",5000,1};
		Object[] array = qr.query("select * from product", new ArrayHandler());
		for (Object object : array) {
			System.out.println(object);
		}
	}
  
  //2:ArrayListHandler
	//负责将结果集中每一行的数据放入数组
	//将多个数组将入集合中:List<Object[]>
	@Test
	public void func2() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		List<Object[]> list = qr.query("select * from product", new ArrayListHandler());
		for (Object[] objects : list) {
			for (Object object : objects) {
				System.out.print(object+"\t");
			}
			System.out.println();
		}
	}
  
  //3:BeanHandler
	//负责将结果集中第一行的数据放入Java Bean对象
	@Test
	public void func3() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		Product p = qr.query("select * from product", new BeanHandler<Product>(Product.class));
		System.out.println(p);
	}
  
  //4:BeanListHandler
	//负责将结果集中每一行的数据放入Java Bean对象
	//多个Java Bean对象存入集合:List<Product>
	@Test
	public void func4() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		List<Product> list = qr.query("select * from product", new BeanListHandler<Product>(Product.class));
		for (Product product : list) {
			System.out.println(product);
		}
	}
  
  //5:ColumnListHandler
	//将某一列的数据存入集合
	@Test
	public void func5() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		//可以获取某一列或多列的值
		List<Object> list = qr.query("select * from product where pid = 6", new ColumnListHandler("pname"));
		for (Object object : list) {
			System.out.println(object);
		}
	}
  
  	
	//6:MapHandler
	//将负责将结果集中第一行的数据放入Map集合
	//Map键:列的名字
	//Map值:列的值
	@Test
	public void func6() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		Map<String, Object> map = qr.query("select * from product", new MapHandler());
		System.out.println(map);
	}

	//7:MapListHander
	//将负责将结果集中每一行的数据放入Map集合
	//Map多了,存入集合: List<Map<String,Object>>
	@Test
	public void func7() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		List<Map<String, Object>> list = qr.query("select * from product", new MapListHandler());
		for (Map<String, Object> map : list) {
			System.out.println(map);
		}
	}
	
	//8:ScalarHandler
	//可以获取单值:sum(),count()
	@Test
	public void func8() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		Object object = qr.query("select sum(price) ‘s‘ ,count(*) ‘c‘from product", new ScalarHandler("c"));
		
		System.out.println(object);
	}
		

apache-dbutils

上一篇:mysql -- MAC 安装配置mysql


下一篇:mysql MVCC