По какой-то причине я возвращаю только 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;
}
Первая проблема решена с помощью этого кода
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(person);
}
//System.out.println("Persons Size: " + persons.size());
return persons;
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
Первый возврат printn
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);
System.out.println(json);
writer.write(json);
}
}
return true;
}
Второй возврат printn
private Person extractUserFromResultSet(ResultSet rs) throws SQLException {
person.setId(rs.getInt("ID"));
person.setFName(rs.getString("FName"));
person.setLName(rs.getString("LName"));
person.setDOB(rs.getString("DOB"));
person.setDepartment(rs.getString("Department"));
person.setLocation(rs.getString("Location"));
person.setCellNr(rs.getString("Cellnr"));
person.setUsername(rs.getString("Username"));
person.setPassword(rs.getString("Password"));
return person;
}