Your updated code looks much better. You still moved where the code gets the Connection
. I moved that back, and tweaked it a tiny bit. Get the id
, first_name
and last_name
from the ResultSet
. Then instantiate a Person
instance and add it to a List
. Finally, return the List
. Something like,
public ArrayList<Person> getPeople() throws Exception {
Connection conn = this.getCandidateConnection();
/* ---------- DO NOT CHANGE THE CODE ABOVE THIS LINE, IT WILL BE RESET ON RUN ---------- */
final String selectSql = "SELECT id, first_name, last_name " //
+ "FROM people ORDER BY last_name, first_name";
ArrayList<Person> al = new ArrayList<>();
try (Statement statement = conn.createStatement(); //
ResultSet resultSet = statement.executeQuery(selectSql)) {
while (resultSet.next()) {
int id = resultSet.getInt("id");
String firstName = resultSet.getString("first_name");
String lastName = resultSet.getString("last_name");
al.add(new Person(id, firstName, lastName));
}
} finally {
if (conn != null) {
conn.close();
}
}
return al;
/* ---------- DO NOT CHANGE THE CODE BELOW THIS LINE, IT WILL BE RESET WHEN RUNNING ---------- */
}
CLICK HERE to find out more related problems solutions.