From 6abe846e8a8e30755afbed519ae8d78b35ac7a55 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Mon, 24 Apr 2017 15:25:51 +0200 Subject: [PATCH] Added ability to persist persons Signed-off-by: Jim Martens --- 02/src/de/dis2017/data/db/ORM.java | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/02/src/de/dis2017/data/db/ORM.java b/02/src/de/dis2017/data/db/ORM.java index 491e801..debf17e 100644 --- a/02/src/de/dis2017/data/db/ORM.java +++ b/02/src/de/dis2017/data/db/ORM.java @@ -697,4 +697,49 @@ public class ORM { e.printStackTrace(); } } + + /** + * Persists the given person. + * + * @param person the person that should be persisted + */ + public void persist(Person person) + { + try { + if (person.getId() == -1) { + String insertSQL = "INSERT INTO PERSON (firstName, name, address) VALUES (?, ?, ?)"; + PreparedStatement pstmt = _connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS); + + pstmt.setString(1, person.getFirstName()); + pstmt.setString(2, person.getName()); + pstmt.setString(3, person.getAddress()); + pstmt.executeUpdate(); + + ResultSet rs = pstmt.getGeneratedKeys(); + if (rs.next()) { + person.setId(rs.getInt(1)); + } + + rs.close(); + pstmt.close(); + } else { + // create query + String updateSQL = "UPDATE PERSON SET firstName = ?, name = ?, address = ? WHERE ID = ?"; + PreparedStatement pstmt = _connection.prepareStatement(updateSQL); + pstmt.setString(1, person.getFirstName()); + pstmt.setString(2, person.getName()); + pstmt.setString(3, person.getAddress()); + pstmt.setInt(4, person.getId()); + + // execute query + pstmt.executeUpdate(); + pstmt.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + if (!_persons.containsKey(person.getId())) { + _persons.put(person.getId(), person); + } + } }