他包含BeanHandle、BeanListHandle和ScalarHandle

BeanHandle、BeanListHandle

Beanhandle:将一行数据转换成Javabean对象

BeanListHandle:遍历多行数据,将每一行数据转换成javaben对象,并且添加到list集合中

测试过程

用户类

package p01;

public class User {
	private int id ;
	private String name;
	private String password;
	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;
	}
}

数据库操纵类——BaseDao类

package p01;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.dbutils.ResultSetHandler;

public class BaseDao {
	public static Object query(String sql,ResultSetHandler<?> rsh,Object...params) {
		Connection con = null ;
		PreparedStatement ps = null ; 
		ResultSet rs = null;
		try {
			//获取连接
			con = JDBC_Utils.getCon();
			//预编译sql
			ps = con.prepareStatement(sql);
			//给sql语句占位符赋值
			for(int i =0;params!=null && i<params.length;i++) {
				ps.setObject(i+1, params[i]);
			}
			//执行sql语句,如果使用的是预编译对象那么请不要加入参数
			rs = ps.executeQuery();
			//处理rs结果集
			Object obj = rsh.handle(rs);
			return obj;
			
		} catch (Exception e) {
			e.printStackTrace();
		
		}finally {
			JDBC_Utils.realse(rs, ps, con);
		}
		return rs;
	}
}

数据库连接工具——JDBCUtils

package p01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class JDBC_Utils {
	//获取连接对象的方法
	public static Connection getCon(){
		Connection con = null ;
		try {
			//注册对象
			Class.forName("com.mysql.jdbc.Driver");
			//连接
			String jdbc_url = "jdbc:mysql://localhost:3306/jdbc";
			String jdbc_user = "root";
			String jdbc_password = "root";
			con = DriverManager.getConnection(jdbc_url,jdbc_user,jdbc_password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	//关闭连接,释放资源
	public static void realse(ResultSet rs,Statement sta,Connection con) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs = null;
		}
		if(sta!=null) {
			try {
				sta.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			sta = null ;
		}
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			con = null;
		}
		
	}
}

BeanHandle测试类

package p01;

import org.apache.commons.dbutils.handlers.BeanHandler;

public class ResultsetTest_1_Query {
	public static void testBeanHander() {
		//数据库连接工作
		BaseDao basedao = new BaseDao();
		String sql = "select * from users where id = ?";
		//最后可以传入多个参数,参数的个数看占位符的个数——
		User u = (User) basedao.query(sql, new BeanHandler<User>(User.class), 1);
		System.out.println(u.getName());
	}
	public static void main(String[] args) {
		testBeanHander();
	}
}

BeanListHandle测试类

package p01;

import java.util.ArrayList;

import org.apache.commons.dbutils.handlers.BeanListHandler;

public class ResultsetTest_2_List {
	public static void testBeanListHander() {
		//数据库连接工作
		BaseDao basedao = new BaseDao();
		String sql = "select * from users";
		//最后可以传入多个参数,参数的个数看占位符的个数——
		ArrayList<User> u = (ArrayList<User>) basedao.query(sql, new BeanListHandler<User>(User.class));
		for(int i =0;i<u.size();i++) {
			System.out.println(u.get(i).getName());
		}
	
	}
	public static void main(String[] args) {
		testBeanListHander();
	}
}

ScalarHandle

ScalarHandle:将单行单列的值,转换成object对象。

在开发中通常的作用就是处理聚合函数(例如max、min、avg、sum、count)

“select count(*) from table_name”返回单行单列的值

ScalarHandle测试类

package p01;

import java.util.ArrayList;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

public class ResultsetTest_3_ScalarHandler {
	public static void testSchlarHander() {
		//数据库连接工作
		BaseDao basedao = new BaseDao();
		String sql = "select * from users where id = ?";
		Object obj = basedao.query(sql, new ScalarHandler("name"),1);
		System.out.println(obj);
		
		
	
	}
	public static void main(String[] args) {
		testSchlarHander();
	}
}
说点什么
暂时没有人评论
Loading...