Added ability to retrieve a person

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
2017-04-24 14:44:07 +02:00
parent bd18b9f0c8
commit 8453062df9

View File

@ -19,6 +19,7 @@ public class ORM {
private Map<String, EstateAgent> _agentsUsername;
private Map<Integer, Estate> _estates;
private Map<Integer, Contract> _contracts;
private Map<Integer, Person> _persons;
/**
* Initializes the ORM.
@ -30,6 +31,7 @@ public class ORM {
_agentsUsername = new HashMap<>();
_estates = new HashMap<>();
_contracts = new HashMap<>();
_persons = new HashMap<>();
}
/**
@ -359,6 +361,46 @@ public class ORM {
return null;
}
/**
* Loads the person with the given ID from database and returns the corresponding object.
*
* @param ID the ID of the person to load
* @return the Person or null if there is no such agent
*/
public Person getPerson(int ID) {
if (_persons.containsKey(ID)) {
return _persons.get(ID);
}
try {
// create query
String selectSQL = "SELECT * FROM PERSON WHERE ID = ?";
PreparedStatement pstmt = _connection.prepareStatement(selectSQL);
pstmt.setInt(1, ID);
ResultSet rs = pstmt.executeQuery();
Person person;
if (rs.next()) {
person = new Person();
person.setId(rs.getInt("ID"));
person.setFirstName(rs.getString("firstName"));
person.setName(rs.getString("name"));
person.setAddress(rs.getString("address"));
rs.close();
pstmt.close();
_persons.put(person.getId(), person);
return person;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* Loads the estate agent with the given username from database and returns the corresponding object,
*