模拟DBUtil完成数据单个对象查询操作

DBUtils是一个非常优秀的JDBC封装框架,他将传统的JDBC里面的Statement和ResultSet封装成一起,根据结果进行返回,返回的结果有特定对象,特定对象集合,映射,映射集合,那么本文章通过模拟DButils的query方法进行查询单个特定对象操作,代码如下(感谢宁神的启发):
注意:为了读者更方便理解代码,请先学习DButils
MyQueryRunner

package com.baidu.util;

import org.apache.commons.dbutils.ResultSetHandler;

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

public class MyQueryRunner {

    public <T> T query(Connection connection, String sql, ResultSetHandler<T> handler,Object... params){
        T obj = null;
        if(connection==null){
            throw new NullPointerException("connection is null");
        }else if (sql==null){
            throw new NullPointerException("sql is null");
        }else if(handler==null){
            throw new NullPointerException("handler is null");
        }else{
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement=connection.prepareStatement(sql);
                for (int i=0;i<params.length;i++){
                    preparedStatement.setObject((i+1),params[i]);
                }
                resultSet = preparedStatement.executeQuery();
                obj=handler.handle(resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(preparedStatement!=null){
                        preparedStatement.close();
                    }
                    if(resultSet!=null){
                        resultSet.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return  obj;
    }
}

MyBeanHanbler

package com.baidu.util;

import org.apache.commons.dbutils.ResultSetHandler;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class MyBeanHandler<T> implements ResultSetHandler<T> {
    private Class aClass;
    public MyBeanHandler(Class aClass){
        this.aClass =aClass;
    }

    @Override
    public  T handle(ResultSet resultSet) throws SQLException {
        T obj = null;
        try {
            Constructor constructor = aClass.getConstructor();
            obj = (T)constructor.newInstance();//创建对象
            ResultSetMetaData metaData = resultSet.getMetaData();//获取Mtatadata对象
            int columnCount = metaData.getColumnCount(); //获取信息个数
            if(resultSet.next()){
                for(int i =1;i<=columnCount;i++){
                    String columnName = metaData.getColumnName(i);
//                    String columnClassName = metaData.getColumnClassName(i);
                    Object columValue = resultSet.getObject(i);
                    String columName = "set"+columnName.substring(0,1).toUpperCase()+columnName.substring(1);
                    Method method = null;
                    try {
                        method = aClass.getMethod(columName,columValue.getClass());

                    } catch (NoSuchMethodException e) {
                    } catch (NullPointerException e){

                    }

                    if(method!=null){
                        method.invoke(obj,columValue);
                    }
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return obj;
    }
}

你可能感兴趣的:(java)