【发布时间】:2020-04-27 08:57:18
【问题描述】:
我正在做一个使用 spring 的项目。 我有一项服务,它将对象“婴儿”的列表返回给模型属性。 由于某种原因,在遍历循环并将对象添加到列表后,我得到 list.IsEmpty() 为真。我在这里做错了什么? 我的控制器代码:
@GetMapping("/print")
public String print(Model model) throws SQLException {
System.out.println(email);
ResultSet rs=DB.resultset(email);
model.addAttribute("items", DB.createList(rs));
return "contribution";
}
我的数据库服务代码:
public List<Baby> createList(ResultSet resultSet) throws SQLException {
int count=0,i=0;
ResultSet rs=resultSet;
while(rs.next()){
count++;
}
System.out.println(count);
Baby baby[]=new Baby[count];
List<Baby> list = new ArrayList<Baby>(count);
while (resultSet.next()) {
i=0;
baby[i].setId(resultSet.getString("nameId"));
baby[i].setName(resultSet.getString("name"));
baby[i].setReligion(resultSet.getString("religion"));
baby[i].setLocation(resultSet.getString("location"));
list.add(baby[i]);
i++;
System.out.println(i);
}
System.out.println(list.isEmpty());
return list;
}
婴儿班代码:
package com.example.demo;
public class Baby {
private String id;
private String name;
private String religion;
private String location;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLocation() {
return location;
}
public String getReligion() {
return religion;
}
public void setLocation(String location) {
this.location = location;
}
public void setReligion(String religion) {
this.religion = religion;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
【问题讨论】:
-
显示
resultset()的源代码。 -
没有
NullPointerException,您的代码如何运行?你什么时候真正在你的数据结构中添加了新的婴儿(baby和list)? -
创建 arrayList 时
count的值是多少? -
@deHaar 数据库中有一个表,其中包含所有婴儿的名字......这不是问题
标签: java mysql spring spring-boot