diff --git a/02/src/de/dis2017/data/db/ORM.java b/02/src/de/dis2017/data/db/ORM.java index b148acf..a7cd9d2 100644 --- a/02/src/de/dis2017/data/db/ORM.java +++ b/02/src/de/dis2017/data/db/ORM.java @@ -101,6 +101,35 @@ public class ORM { return null; } + /** + * Deletes the given agent from the database. + * + * @param agent the agent that should be deleted + */ + public void delete(EstateAgent agent) + { + try { + if (agent.getId() == -1) { + System.err.println("This agent is not yet persisted to the dabase and cannot be deleted."); + return; + } else { + // create query + String updateSQL = "DELETE FROM ESTATEAGENT WHERE ID = ?"; + PreparedStatement pstmt = _connection.prepareStatement(updateSQL); + pstmt.setInt(1, agent.getId()); + + // execute query + pstmt.executeUpdate(); + pstmt.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + if (_agents.containsKey(agent.getId())) { + _agents.remove(agent.getId(), agent); + } + } + /** * Persists the given agent. *