Java对JDBC结果集ResultSet的处理

代码中的Json及JsonList为本人重新创建的类,参考代码时可转为自己常用的相应类
/**
     * 将结果集转化为json对象
     *
     * @param resultSet 操作数据库后的结果集
     * @return json类型的结果集
     * @throws SQLException sql异常
     */
    protected static Json getResultSetByJson(ResultSet resultSet) throws SQLException {
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        Json json = new Json();
        while (resultSet.next()) {
            for (int i = 1; i <= columnCount; i++) {
                String key = resultSetMetaData.getColumnLabel(i);
                String value = resultSet.getString(key);
                json.put(key, value);
            }
        }
        return json;
    }

    /**
     * 将结果集转化为JosnList对象
     *
     * @param resultSet 结果集
     * @return jsonlist类型的结果集
     * @throws SQLException sql异常
     */
    protected static JsonList getResultSetByJsonList(ResultSet resultSet) throws SQLException {
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        JsonList jsonList = new JsonList();
        while (resultSet.next()) {
            Json json = new Json();
            for (int i = 1; i <= columnCount; i++) {
                String key = resultSetMetaData.getColumnLabel(i);
                String value = resultSet.getString(key);
                json.put(key, value);
            }
            jsonList.add(json);
        }
        return jsonList;
    }

    /**
     * 将结果集映射为实体类
     *
     * @param resultSet 结果集
     * @param t         实体
     * @param <T>       泛型
     * @return 实体类型的结果集
     * @throws SQLException              sql异常
     * @throws InvocationTargetException 反射异常
     * @throws IllegalAccessException    反射时访问私有成员权限不足导致的异常
     * @throws NoSuchMethodException     得不到方法异常
     */
    protected static <T> T getResultSetByT(ResultSet resultSet, T t) throws SQLException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        //获取method方法
        Method[] methods = t.getClass().getMethods();

        //获取ResultSet列名
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        String[] columnNames = new String[columnCount];
        for (int i = 0; i < columnCount; i++) {
            columnNames[i] = resultSetMetaData.getColumnLabel(i + 1);
        }
        //遍历ResultSet
        while (resultSet.next()) {
            for (int i = 0; i < columnNames.length; i++) {
                //取得set方法
                String setMethodName = "set" + columnNames[i];
                //遍历method方法
                for (int j = 0; j < methods.length; j++) {
                    if (methods[j].getName().equalsIgnoreCase(setMethodName)) {
                        setMethodName = methods[j].getName();
                        Object value = resultSet.getObject(columnNames[i]);

                        //通过set方法赋值到实体
                        try {
                            if (value != null) {
                                //实体字段类型和RsultSet一样时
                                Method method = t.getClass().getMethod(setMethodName, value.getClass());
                                method.invoke(t, value);
                            }
                        } catch (Exception e) {
                            //实体字段类型和ResultSet不一样时,转为String
                            Method method = t.getClass().getMethod(setMethodName, String.class);
                            method.invoke(t, value.toString());
                        }
                    }
                }
            }
        }
        return t;
    }

发表回复

您的电子邮箱地址不会被公开。

20 + 1 =