From 144f9cdaf1234b466771e4b74219533afcf43413 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Mon, 3 Feb 2020 16:51:20 +0100 Subject: [PATCH] Fixed removing lifts (DB) --- .../V10Lift/Managers/DBManager.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java index 7166c8a..9b5c4bb 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java @@ -45,12 +45,28 @@ public class DBManager { } public void save() { - Gson gson = new Gson(); - for (Map.Entry entry : DataManager.getLifts().entrySet()) { + try { + Gson gson = new Gson(); - byte[] blob = gson.toJson(entry.getValue()).getBytes(); + String query0 = "SELECT * FROM lifts"; + PreparedStatement statement0 = data.getConnection().prepareStatement(query0); + ResultSet liftSet = statement0.executeQuery(); + while (liftSet.next()) { + if (!DataManager.containsLift(liftSet.getString("liftName"))) { + Bukkit.getLogger().info("[V10Lift] Removing lift " + liftSet.getString("liftName") + " to data..."); + + String query = "DELETE FROM lifts WHERE liftName = ?"; + PreparedStatement statement = data.getConnection().prepareStatement(query); + statement.setString(1, liftSet.getString("liftName")); + statement.executeUpdate(); + } + } + + for (Map.Entry entry : DataManager.getLifts().entrySet()) { + byte[] blob = gson.toJson(entry.getValue()).getBytes(); + + Bukkit.getLogger().info("[V10Lift] Saving lift " + entry.getKey() + " to data..."); - try { String query = "INSERT INTO lifts (liftName, liftData) VALUES (?, ?)"; PreparedStatement statement = data.getConnection().prepareStatement(query); statement.setString(1, entry.getKey()); @@ -62,10 +78,8 @@ public class DBManager { statement2.setBytes(1, blob); statement2.setString(2, entry.getKey()); statement2.executeUpdate(); - - Bukkit.getLogger().info("[V10Lift] Saving lift " + entry.getKey() + " to data..."); - } catch(SQLException ignored) {} - } + } + } catch(SQLException ignored) {} } public void closeConnection() {