All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zhuang.data.jdbc.util.ResultSetUtils Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.zhuang.data.jdbc.util;

import com.zhuang.data.orm.annotation.UnderscoreNaming;
import com.zhuang.data.util.StringUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ResultSetUtils {

    public static  List readToEntities(ResultSet resultSet, Class entityType) throws SQLException {
        List result = new ArrayList();
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        try {
            while (resultSet.next()) {
                T entity;
                entity = entityType.newInstance();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultSetMetaData.getColumnName(i);
                    columnName = StringUtils.underscoreToCamelCase(columnName);
                    Object value = resultSet.getObject(i);
                    PropertyDescriptor propertyDescriptor = new PropertyDescriptor(columnName, entityType);
                    Method setterMethod = propertyDescriptor.getWriteMethod();
                    setterMethod.invoke(entity, value);
                }
                result.add(entity);
            }
        } catch (Exception e) {
            throw new RuntimeException("ResultSetUtils.readToList执行失败!", e);
        }
        return result;
    }

    public static List readToMaps(ResultSet resultSet) throws SQLException {
        List result = new ArrayList<>();
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        try {
            while (resultSet.next()) {
                Map map = new LinkedHashMap();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultSetMetaData.getColumnName(i);
                    columnName = StringUtils.underscoreToCamelCase(columnName);
                    Object value = resultSet.getObject(i);
                    map.put(columnName, value);
                }
                result.add(map);
            }
        } catch (Exception e) {
            throw new RuntimeException("ResultSetUtils.readToList执行失败!", e);
        }
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy