zoukankan      html  css  js  c++  java
  • QueryRunner类 的應用,以及ResultSetHandler 接口的实现类

    1 .该接口用于处理 java.sql.ResultSet,将数据按要求转换为另一种形式。
      ResultSetHandler 接口提供了一个单独的方法:Object handle (java.sql.ResultSet .rs)。
    2.ResultSetHandler 接口的实现类

    ArrayHandler:把结果集中的第一行数据转成对象数组。
    ArrayListHandler:把结果集中的每一行数据都转成一个数组,再存放到List中。
    BeanHandler:将结果集中的第一行数据封装到一个对应的JavaBean实例中。
    BeanListHandler:将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。
    ColumnListHandler:将结果集中某一列的数据存放到List中。
    KeyedHandler(name):将结果集中的每一行数据都封装到一个Map<列名,列值>里,再把这些map再存到一个map里,其key为指定的key。
    MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
    MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List


    3.示例代碼如下:

      1.首先定義一實體類

    package com.beiwo.day05.entity;
    
    import java.io.Serializable;
    import java.sql.Date;
    
    public class Person implements Serializable{
    
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	private int id;
    	
    	private String name;
    	
    	private String password;
    	
    	private String email;
    	
    	private Date birthday;
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public String getEmail() {
    		return email;
    	}
    
    	public void setEmail(String email) {
    		this.email = email;
    	}
    
    	public Date getBirthday() {
    		return birthday;
    	}
    
    	public void setBirthday(Date birthday) {
    		this.birthday = birthday;
    	}
    
    	public static long getSerialversionuid() {
    		return serialVersionUID;
    	}
    
    	@Override
    	public String toString() {
    		return "Person [id=" + id + ", name=" + name + ", password=" + password
    				+ ", email=" + email + ", birthday=" + birthday + ", getId()="
    				+ getId() + ", getName()=" + getName() + ", getPassword()="
    				+ getPassword() + ", getEmail()=" + getEmail()
    				+ ", getBirthday()=" + getBirthday() + ", getClass()="
    				+ getClass() + ", hashCode()=" + hashCode() + ", toString()="
    				+ super.toString() + "]";
    	}
    	
    	
    	
    }
    

     測試查詢

    package com.beiwo.day05.test;
    
    import java.sql.SQLException;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.ArrayHandler;
    import org.apache.commons.dbutils.handlers.ArrayListHandler;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.apache.commons.dbutils.handlers.ColumnListHandler;
    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.beiwo.day05.entity.Person;
    import com.beiwo.day05.util.C3P0Util;
    
    
    public class TestDBUtils {
    	
    	@Test //把结果集中的每一行数据都转成一个数组,再存放到List中。
    
    
    	public void testArrayListHandler() throws SQLException{
    		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "SELECT * FROM person";
    		
    		List<Object[]> list = qr.query(sql, new ArrayListHandler());
    		
    		for (Object[] objects : list) {
    			for (Object object : objects) {
    				System.out.println(object);
    			}
    			System.out.println("-----------------------------");
    		}
    	}
    	
    	
    	
    	@Test //把结果集中的第一行数据转成对象数组。
    
    	public void testArrayHandler() throws SQLException{
    		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "SELECT * FROM person";
    		
    		Object[] object = qr.query(sql, new ArrayHandler());
    		for(Object object1 : object){
    			System.out.println(object1);
    		}
    	
    	}
    	
    //	@Test 将结果集中某一列的数据存放到List中。
    	public void testColumnListHandler() throws SQLException{
    		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "SELECT name,password,email,birthday FROM person";
    		
    		List<Object> persons = qr.query(sql, new ColumnListHandler(2));
    		for(Object object : persons){
    			System.out.println(object);
    		}
    	
    	}
    	
    	@Test //将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
    
    	public void testMapHandler() throws SQLException{
    		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "SELECT * FROM person WHERE id=?";
    		
    		Map<String, Object> map= qr.query(sql, new MapHandler(),1);
    		for (Map.Entry<String, Object> m:map.entrySet()) {
    			System.out.println(m.getKey() + "	" + m.getValue());
    		}
    	
    	}
    	
    		
    	@Test //将结果集中的每一行数据都封装到一个Map里,然后再存放到List
    
    	public void testMapListHandler() throws SQLException{
    		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "SELECT * FROM person ";
    		
    		List<Map<String, Object>> list = qr.query(sql, new MapListHandler());
    		for (Map<String, Object> map : list) {
    			for (Map.Entry<String, Object> m : map.entrySet()) {
    				System.out.println(m.getKey() + "	" + m.getValue());
    			}
    			System.out.println("--------------------------------------------");
    		}
    	
    	}
    	
    	
    	@Test //查询数据库中信息的总条数
    	public void testScalarHandler() throws SQLException{
    		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "SELECT COUNT(*) FROM person ";
    		
    	Long l = (Long)qr.query(sql, new ScalarHandler(1));
    	System.out.println(l);
    	
    	}
    	
    	
    	@Test //将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。
    
    	public void testBeanListHandler() throws SQLException{
    		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "SELECT * FROM person ";
    		
    	List<Person> list = qr.query(sql, new BeanListHandler<Person>(Person.class));
    	System.out.println(list);
    	}
    	
    	
    	@Test //将结果集中的第一行数据封装到一个对应的JavaBean实例中。
    	public void testBeanHandler() throws SQLException{
    		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "SELECT * FROM person WHERE id=?";
    		
    	Person person = qr.query(sql, new BeanHandler<Person>(Person.class),1);
    	System.out.println(person);
    	}
    }
    

     測試增刪改

    package com.beiwo.day05.test;
    
    import java.sql.SQLException;
    import java.util.Date;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.junit.Test;
    
    import com.beiwo.day05.util.C3P0Util;
    
    public class TestUpDate {
    
    	@Test
    	public void testInsert() throws SQLException{
    		QueryRunner qRunner = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "INSERT INTO person(name,password,email,birthday)VALUES(?,?,?,?)";
    		qRunner.update(sql, "ccc","789","789@qq.com",new Date());
    	}
    	
    	@Test
    	public void testUpDate1() throws SQLException{
    		QueryRunner qRunner = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "UPDATE person SET name='www' WHERE id=2";
    		qRunner.update(sql);
    	}
    	
    	@Test
    	public void testUpDate2() throws SQLException{
    		QueryRunner qRunner = new QueryRunner(C3P0Util.getDataSource());
    		String sql = "DELETE FROM person WHERE id=6";
    		qRunner.update(sql);
    	}
    	
    }
    
  • 相关阅读:
    猜数字和楼层扔鸡蛋问题
    python+selenium+unittest----skip(跳过用例)
    python+selenium+unittest----常用属性详解(框架属性详解)
    php----等比缩放图片
    Vue----生命周期
    js----定义变量的几种方式
    quartz(7)-源码分析
    quartz(6)--集群
    quartz(5)--作业管理和存储
    quartz(4)--quartz.properties文件
  • 原文地址:https://www.cnblogs.com/houjiie/p/6192578.html
Copyright © 2011-2022 走看看