当前位置:Gxlcms >
数据库问题 >
DBUtiles中的简单使用(QueryRunner和ResultSetHandler的手动实现)
DBUtiles中的简单使用(QueryRunner和ResultSetHandler的手动实现)
时间:2021-07-01 10:21:17
帮助过:24人阅读
- QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
- runner.update("insert into account values(null,?,?)","e",888);
- runner.update("update account set money=0 where name=?", "e");
查询的方法稍微麻烦一点,因为我们需要对查询到的结果集进行设置。通常需要把结果集ResultSet封装到JavaBean或者集合或者数组中。
查看一个方法: <T> T query(String sql, ResultSetHandler<T> rsh, Object... params)
这里第一个参数是sql语句字符串,第二个参数是一个实现了ResultSetHandler接口的类对象,第三个参数是Object类型的可变参数。返回值是一个T类型。
如果我们用的eclipse或者MyEclipse 鼠标放到ResutlSetHandlet上面,按F2,会有针对T的说明。<T> the target type the input ResultSet will be converted to.
意思是,T 代表 ResultSet结果集要装入的目标类型。也就是我们前面提到的数组,集合,甚至javabean.
下面用一段代码来实现把结果集装入一个List数组中。其中Account是一个javaBean,符合account表。
[java] view plain
copy
- public static List test2() throws Exception{
- QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
- return runner.query("select * from account where name=?",new ResultSetHandler<List<Account>>(){
- public List<Account> handle(ResultSet rs) throws SQLException {
- List<Account> list = new ArrayList<Account>();
- while(rs.next()){
- Account acc = new Account();
- acc.setId(rs.getInt("id"));
- acc.setName(rs.getString("name"));
- acc.setMoney(rs.getDouble("money"));
- list.add(acc);
- }
-
- return list;
- }
- } , "a");
- }
接下来,我们用两段代码来模拟QueryRunner和ResultSetHandler的实现原理。
[java] view plain
copy
- package cn.itheima.dbutils;
-
- import java.sql.Connection;
- import java.sql.ParameterMetaData;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
-
- import javax.sql.DataSource;
-
- import cn.itheima.domain.Account;
-
- import com.mchange.v2.c3p0.ComboPooledDataSource;
-
- public class MyQueryRunner {
- private DataSource source = null;
- public MyQueryRunner(DataSource source) {
- this.source = source;
- }
-
-
- public <T> T query(String sql,MyResultSetHandler<T> handler,Object ...objs){
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- conn = source.getConnection();
- ps = conn.prepareStatement(sql);
- ParameterMetaData metaData = ps.getParameterMetaData();
- for(int i=1;i<=metaData.getParameterCount();i++){
- ps.setObject(i, objs[i-1]);
- }
-
- rs = ps.executeQuery();
- return handler.handle(rs);
-
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException();
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- rs = null;
- }
- }
- if (ps != null) {
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- ps = null;
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- conn = null;
- }
- }
- }
-
- }
-
-
- public int update(String sql,Object ...objs){
- Connection conn = null;
- PreparedStatement ps = null;
- try{
- conn = source.getConnection();
- ps = conn.prepareStatement(sql);
- ParameterMetaData metaData = ps.getParameterMetaData();
- for(int i=1;i<=metaData.getParameterCount();i++){
- ps.setObject(i, objs[i-1]);
- }
-
- return ps.executeUpdate();
- }catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException();
- }finally{
- if(ps!=null){
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- ps = null;
- }
- }
- if(conn!=null){
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- conn = null;
- }
- }
- }
- }
- }
MyResultSetHandler接口
[java] view plain
copy
- package cn.itheima.dbutils;
-
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public interface MyResultSetHandler <T>{
- T handle(ResultSet rs)throws SQLException;
- }
当然,实际应用中没有这么麻烦。因为DBUtils已经帮我们实现了很多ResultSetHandler的实现类。通过这些类可以很方便的对结果集进行封装。
ResultSetHandler的实现类
[java] view plain
copy
-
- Object[] objs = runner.query("select * from account where name=?",new ArrayHandler() , "c");
- System.out.println(objs);
-
-
-
- List<Object[]> list = runner.query("select * from account",new ArrayListHandler() );
- System.out.println(list);
-
-
-
- public static List test2() throws Exception{
- QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
- return runner.query("select * from account where name=?",new ResultSetHandler<List<Account>>(){
- public List<Account> handle(ResultSet rs) throws SQLException {
- List<Account> list = new ArrayList<Account>();
- while(rs.next()){
- Account acc = new Account();
- acc.setId(rs.getInt("id"));
- acc.setName(rs.getString("name"));
- acc.setMoney(rs.getDouble("money"));
- list.add(acc);
- }
- return list;
- }
- } , "a");
- }
-
-
-
- Account acc = runner.query("select * from account where name=?",new BeanHandler<Account>(Account.class) , "c");
- System.out.println(acc);
-
-
-
- List<Account> acclist = runner.query("select * from account",new BeanListHandler<Account>(Account.class) );
- System.out.println(acclist);
-
-
-
- Map map = runner.query("select * from account",new MapHandler() );
- System.out.println(map);
-
-
-
- List<Map<String, Object>> maplist = runner.query("select * from account",new MapListHandler() );
- System.out.println(maplist);
-
-
-
- List<Object> columnList = runner.query("select * from account",new ColumnListHandler(2) );
- System.out.println(columnList);
-
-
-
- Map<Object, Map<String, Object>> keymap = runner.query("select * from account",new KeyedHandler("id") );
- System.out.println(keymap);
-
-
-
-
- Long count = (Long)runner.query("select count(*) from account",new ScalarHandler(1) );
- System.out.println(count);
DBUtiles中的简单使用(QueryRunner和ResultSetHandler的手动实现)
标签:for cli ack base ase fork img lis number