出于某种原因,我只返回 1 个条目,但我有 2 个待处理的用户。
public Set getApplicationStatus() {
DbConn connector = new DbConn();
String Pending = "Pending";
Connection connection = connector.getConnection();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM `person` WHERE `Department` = '"+Pending+"'");
Set persons = new HashSet();
while (rs.next()) {
Person person = extractUserFromResultSet(rs);
persons.add(person);
}
System.out.println(persons.size());
return persons;
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
如果我直接在数据库上运行查询,我会得到两个帐户,但如果我通过,ResultSet我似乎只得到最后一个。
public static Person p = new Person();
public static ArrayList<Person> Users = new ArrayList();
public boolean WriteTOfile() throws IOException {
try (Writer writer = new FileWriter("Output.json")) {
Gson gson = new GsonBuilder().create();
Users = p.getApplicationStatus();
for (Person User : Users) {
String json = gson.toJson(User);
writer.write(json);
}
}
return true;
}
当我写入我的 Json 文件时,它会将最后一行添加两次?
public ArrayList<Person> getApplicationStatus() {
DbConn connector = new DbConn();
String Pending = "Pending";
Connection connection = connector.getConnection();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM `person` WHERE `Department` = '"+Pending+"'");
ArrayList persons = new ArrayList();
while (rs.next()) {
Person person = extractUserFromResultSet(rs);
persons.add(person);
}
//System.out.println("Persons Size: " + persons.size());
return persons;
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
幕布斯6054654
相关分类