我写了一段代码,应该通过记录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.copyOf和Arrays.asListDIDNT HELP(因为它不是默认数组)。
2条答案
按热度按时间uujelgoq1#
我为
java.sql.Array
类型创建了一个简单的实用方法:字符串
使用方法:
型
它现在依赖于列
students
中的数据。它是一个字符串数组吗?你的Student
类看起来像什么?wmomyfyw2#
尝试将结果数组转换为如下列表
List<Student> students = (List<Student>) rs.getArray("students");