From 1c5cd7b467a84bd4952a973c4ca6b50f6df81857 Mon Sep 17 00:00:00 2001 From: Logan <37521985+LoJoSho@users.noreply.github.com> Date: Thu, 25 Sep 2025 20:03:05 -0500 Subject: [PATCH] chore: minor refactoring of db code --- .../database/types/MySQLData.java | 33 +++++++------------ .../database/types/SQLiteData.java | 10 +++--- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java index 06f9abc4..e0cfa477 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java @@ -38,14 +38,14 @@ public class MySQLData extends SQLData { HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); try { openConnection(); - if (connection == null) throw new NullPointerException("Connection is null"); + if (connection == null) throw new IllegalStateException("Connection is null"); try (PreparedStatement preparedStatement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `COSMETICDATABASE` " + "(UUID varchar(36) PRIMARY KEY, " + "COSMETICS MEDIUMTEXT " + ");")) { preparedStatement.execute(); } - } catch (SQLException | NullPointerException e) { + } catch (SQLException | IllegalStateException e) { plugin.getLogger().severe(""); plugin.getLogger().severe(""); plugin.getLogger().severe("MySQL DATABASE CAN NOT BE REACHED."); @@ -72,16 +72,10 @@ public class MySQLData extends SQLData { } private void openConnection() throws SQLException { - // Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> { - // ... - // }); - // connection = DriverManager.getConnection("jdbc:mysql://" + DatabaseSettings.getHost() + ":" + DatabaseSettings.getPort() + "/" + DatabaseSettings.getDatabase(), setupProperties()); - // Connection isn't null AND Connection isn't closed :: return try { - if (isConnectionOpen()) { - return; - } else if (connection != null) close(); // Close connection if still active + if (isConnectionOpen()) return; + if (connection != null) close(); // Close connection if still active } catch (RuntimeException e) { e.printStackTrace(); // If isConnectionOpen() throws error } @@ -90,17 +84,15 @@ public class MySQLData extends SQLData { try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, setupProperties()); - } catch (SQLException e) { + } catch (SQLException | ClassNotFoundException e) { System.out.println(e.getMessage()); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); } } public void close() { Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> { try { - if (connection == null) throw new NullPointerException("Connection is null"); + if (connection == null) throw new IllegalStateException("Connection is null"); connection.close(); } catch (SQLException | NullPointerException e) { System.out.println(e.getMessage()); @@ -111,13 +103,12 @@ public class MySQLData extends SQLData { @NotNull private Properties setupProperties() { Properties props = new Properties(); - props.put("user", user); - props.put("password", password); - props.put("autoReconnect", "true"); + props.setProperty("user", user); + props.setProperty("password", password); return props; } - private boolean isConnectionOpen() throws RuntimeException { + private boolean isConnectionOpen() { try { return connection != null && !connection.isClosed(); } catch (SQLException e) { @@ -139,12 +130,12 @@ public class MySQLData extends SQLData { } try { - if (connection == null) throw new NullPointerException("Connection is null"); + if (connection == null) throw new IllegalStateException("Connection is null"); ps = connection.prepareStatement(query); - } catch (SQLException | NullPointerException e) { + } catch (SQLException | IllegalStateException e) { e.printStackTrace(); } return ps; } -} +} \ No newline at end of file diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/SQLiteData.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/SQLiteData.java index b2466dfa..ce5e8e0e 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/SQLiteData.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/SQLiteData.java @@ -60,11 +60,6 @@ public class SQLiteData extends SQLData { } private void openConnection() throws SQLException { - // Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> { - // ... - // }); - // connection = DriverManager.getConnection("jdbc:mysql://" + DatabaseSettings.getHost() + ":" + DatabaseSettings.getPort() + "/" + DatabaseSettings.getDatabase(), setupProperties()); - if (connection != null && !connection.isClosed()) return; // Close Connection if still active @@ -84,9 +79,12 @@ public class SQLiteData extends SQLData { @Override public PreparedStatement preparedStatement(String query) { PreparedStatement ps = null; - if (!isConnectionOpen()) MessagesUtil.sendDebugMessages("Connection is not open"); try { + if (!isConnectionOpen()) { + MessagesUtil.sendDebugMessages("Connection is not open"); + openConnection(); + } ps = connection.prepareStatement(query); } catch (SQLException e) { e.printStackTrace();