mysql 如何将Array(ResultSet JDBC)转换为List?

jjhzyzn0  于 11个月前  发布在  Mysql
关注(0)|答案(2)|浏览(158)

我写了一段代码,应该通过记录ID从数据库表中获取一条记录。表(记录)的一列是student对象的集合。如何从记录中提取学生并将其“折叠”到List集合中?
代码:

public Group findObjById(final int id) {
        Group group = new Group();

        try {
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM `course_work`.`group` WHERE `group_id` = " + id + ";");

            while (rs.next()) {
                int innerId = rs.getInt("group_id");
                List<Student> students = rs.getArray("students");
                group = new Group(innerId, students);
            }

            rs.close();
            st.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return group;
    }

字符串
第10行(*.java中的第102行)出现错误。photo <-

List.copyOfArrays.asListDIDNT HELP(因为它不是默认数组)。

uujelgoq

uujelgoq1#

我为java.sql.Array类型创建了一个简单的实用方法:

/**
     * Collects an SQL-Array using the given collector
     * @param array
     * @param valueRetriever
     * @param collector
     * @param <T>
     * @param <A>
     * @param <R>
     * @return
     * @throws SQLException
     */
    public static <T, A, R> R readArray(Array array, ResultSetValueRetriever<T> valueRetriever, Collector<T, A, R> collector) throws SQLException {
        final BiConsumer<A, T> accumulator = collector.accumulator();
        final A container = collector.supplier().get();

        if (array != null) {
            try (ResultSet rs = array.getResultSet()) {
                while (rs.next()) {
                    // 1 => index, 2 => value
                    // because we iterate ordered already, we only care about the value (2)
                    accumulator.accept(container, valueRetriever.get(rs, 2));
                }
            }
        }

        return collector.finisher().apply(container);
    }

    /**
     * Functional interface representing one ResultSet-Method to get the value of a column<p>
     * For example:<p>
     * <pre>
     *     ResultSetValueRetriever<String> getString = ResultSet::getString;
     *     ResultSetValueRetriever<Integer> getInt = ResultSet::getInt;
     * </pre>
     * @param <T> return type
     */
    @FunctionalInterface
    public interface ResultSetValueRetriever<T> {

        T get(ResultSet rs, int index) throws SQLException;
    }

字符串
使用方法:

// jdbc index starts at 1
// the first column of your resultset is an array => rs.getArray(1)
// ResultSet::getInt is used to retrieve items from the array column 
// -> if its an array of ints, use ResultSet::getInt
// -> if its an array of strings, use ResultSet::getString
final List<Integer> ints = readArray(rs.getArray(1), ResultSet::getInt, Collectors.toList());

// you can also use this to collect arrays of arrays etc
// for example, to collect an array of array of strings:
final List<List<String>> strings = readArray(
    rs.getArray(1),
    (subRs, index) -> readArray(subRs.getArray(index), ResultSet::getString, Collectors.toList()),
    Collectors.toList()
);


它现在依赖于列students中的数据。它是一个字符串数组吗?你的Student类看起来像什么?

wmomyfyw

wmomyfyw2#

尝试将结果数组转换为如下列表List<Student> students = (List<Student>) rs.getArray("students");

相关问题