天天看點

JDBC 利用反射及 JDBC 中繼資料編寫通用的查詢方法(7)

public class MyTest {
    public static void test01() throws ClassNotFoundException, SQLException, IllegalAccessException, InstantiationException {
        String driverClass = "com.mysql.jdbc.Driver";
        String jdbcUrl = "jdbc:mysql://localhost:3306/myemployees?useUnicode=true&useSSL=false";
        String user = "root";
        String password = "abcd123456";

        ResultSet resultSet = null;
        Class.forName(driverClass);
        Connection connection = DriverManager.getConnection(jdbcUrl,user,password);
        String sql = "SELECT flow_id flowId, type, id_card idCard, "
                + "exam_card examCard, student_name studentName, "
                + "location, grade " + "FROM examstudent WHERE flow_id = ?";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,3);
        resultSet = preparedStatement.executeQuery();
        Map<String,Object> values = new HashMap<>();

        ResultSetMetaData rsmd = resultSet.getMetaData();
        while (resultSet.next()){
            for(int i = 0; i < rsmd.getColumnCount();i++){
                String columnLabel = rsmd.getColumnLabel(i+1);
                Object columnValue = resultSet.getObject(columnLabel);
                values.put(columnLabel,columnValue);
            }
        }

        Class clazz = Student.class;
        Object object = clazz.newInstance();
        for(Map.Entry<String,Object> entry : values.entrySet()){
            String fieldName = entry.getKey();
            Object fieldValue = entry.getValue();
            ReflectionUtils.setFieldValue(object,fieldName,fieldValue);
        }

        System.out.println(object);
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }

    public static void main(String[] args) {
        try {
            test01();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
}           

複制

//Student類
public class Student {
    private long flowId;
    private Integer type;
    private String idCard;
    private String examCard;
    private String studentName;
    private String location;
    private Integer grade;

    public long getFlowId() {
        return flowId;
    }

    public int getGrade() {
        return grade;
    }

    public int getType() {
        return type;
    }

    public String getExamCard() {
        return examCard;
    }

    public String getLocation() {
        return location;
    }

    public String getIdCard() {
        return idCard;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setExamCard(String examCard) {
        this.examCard = examCard;
    }

    public void setFlowId(int flowId) {
        this.flowId = flowId;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Student{" +
                "flowId=" + flowId +
                ", type=" + type +
                ", idCard='" + idCard + '\'' +
                ", examCard='" + examCard + '\'' +
                ", studentName='" + studentName + '\'' +
                ", location='" + location + '\'' +
                ", grade=" + grade +
                '}';
    }

    public Student(int flowId, int type, String idCard, String examCard,
                   String studentName, String location, int grade) {
        super();
        this.flowId = flowId;
        this.type = type;
        this.idCard = idCard;
        this.examCard = examCard;
        this.studentName = studentName;
        this.location = location;
        this.grade = grade;
    }
    public Student() {
    }
}           

複制

CREATE TABLE IF NOT EXISTS `examstudent`(`flow_id` INT UNSIGNED AUTO_INCREMENT,    `type` INT NOT NULL,    `id_card` VARCHAR(40) NOT NULL,    `exam_card` VARCHAR(40) NOT NULL,    `student_name` VARCHAR(40) NOT NULL,    `location` VARCHAR(40) NOT NULL,    `grade` INT NOT NULL,    PRIMARY KEY ( `flow_id` ) )ENGINE=InnoDB DEFAULT CHARSET=utf8;           

複制

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 反射的 Utils 函數集合
 * 提供通路私有變量, 擷取泛型類型 Class, 提取集合中元素屬性等 Utils 函數
 * @author Administrator
 *
 */
public class ReflectionUtils {


    /**
     * 通過反射, 獲得定義 Class 時聲明的父類的泛型參數的類型
     * 如: public EmployeeDao extends BaseDao<Employee, String>
     * @param clazz
     * @param index
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Class getSuperClassGenricType(Class clazz, int index){
        Type genType = clazz.getGenericSuperclass();

        if(!(genType instanceof ParameterizedType)){
            return Object.class;
        }

        Type [] params = ((ParameterizedType)genType).getActualTypeArguments();

        if(index >= params.length || index < 0){
            return Object.class;
        }

        if(!(params[index] instanceof Class)){
            return Object.class;
        }

        return (Class) params[index];
    }

    /**
     * 通過反射, 獲得 Class 定義中聲明的父類的泛型參數類型
     * 如: public EmployeeDao extends BaseDao<Employee, String>
     * @param <T>
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static<T> Class<T> getSuperGenericType(Class clazz){
        return getSuperClassGenricType(clazz, 0);
    }

    /**
     * 循環向上轉型, 擷取對象的 DeclaredMethod
     * @param object
     * @param methodName
     * @param parameterTypes
     * @return
     */
    public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes){

        for(Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
            try {
                //superClass.getMethod(methodName, parameterTypes);
                return superClass.getDeclaredMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException e) {
                //Method 不在目前類定義, 繼續向上轉型
            }
            //..
        }

        return null;
    }

    /**
     * 使 filed 變為可通路
     * @param field
     */
    public static void makeAccessible(Field field){
        if(!Modifier.isPublic(field.getModifiers())){
            field.setAccessible(true);
        }
    }

    /**
     * 循環向上轉型, 擷取對象的 DeclaredField
     * @param object
     * @param filedName
     * @return
     */
    public static Field getDeclaredField(Object object, String filedName){

        for(Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
            try {
                return superClass.getDeclaredField(filedName);
            } catch (NoSuchFieldException e) {
                //Field 不在目前類定義, 繼續向上轉型
            }
        }
        return null;
    }

    /**
     * 直接調用對象方法, 而忽略修飾符(private, protected)
     * @param object
     * @param methodName
     * @param parameterTypes
     * @param parameters
     * @return
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     */
    public static Object invokeMethod(Object object, String methodName, Class<?> [] parameterTypes,
                                      Object [] parameters) throws InvocationTargetException{

        Method method = getDeclaredMethod(object, methodName, parameterTypes);

        if(method == null){
            throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
        }

        method.setAccessible(true);

        try {
            return method.invoke(object, parameters);
        } catch(IllegalAccessException e) {
            System.out.println("不可能抛出的異常");
        }

        return null;
    }

    /**
     * 直接設定對象屬性值, 忽略 private/protected 修飾符, 也不經過 setter
     * @param object
     * @param fieldName
     * @param value
     */
    public static void setFieldValue(Object object, String fieldName, Object value){
        Field field = getDeclaredField(object, fieldName);

        if (field == null)
            throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");

        makeAccessible(field);

        try {
            field.set(object, value);
        } catch (IllegalAccessException e) {
            System.out.println("不可能抛出的異常");
        } catch (Exception e){
            System.out.println(e);
        }
    }

    /**
     * 直接讀取對象的屬性值, 忽略 private/protected 修飾符, 也不經過 getter
     * @param object
     * @param fieldName
     * @return
     */
    public static Object getFieldValue(Object object, String fieldName){
        Field field = getDeclaredField(object, fieldName);

        if (field == null)
            throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");

        makeAccessible(field);

        Object result = null;

        try {
            result = field.get(object);
        } catch (IllegalAccessException e) {
            System.out.println("不可能抛出的異常");
        }

        return result;
    }
}           

複制