9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-19 15:09:19 +00:00

chore: minor refactoring of db code

This commit is contained in:
Logan
2025-09-25 20:03:05 -05:00
parent 10f8dda150
commit 1c5cd7b467
2 changed files with 16 additions and 27 deletions

View File

@@ -38,14 +38,14 @@ public class MySQLData extends SQLData {
HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance();
try { try {
openConnection(); 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` " + try (PreparedStatement preparedStatement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `COSMETICDATABASE` " +
"(UUID varchar(36) PRIMARY KEY, " + "(UUID varchar(36) PRIMARY KEY, " +
"COSMETICS MEDIUMTEXT " + "COSMETICS MEDIUMTEXT " +
");")) { ");")) {
preparedStatement.execute(); preparedStatement.execute();
} }
} catch (SQLException | NullPointerException e) { } catch (SQLException | IllegalStateException e) {
plugin.getLogger().severe(""); plugin.getLogger().severe("");
plugin.getLogger().severe(""); plugin.getLogger().severe("");
plugin.getLogger().severe("MySQL DATABASE CAN NOT BE REACHED."); plugin.getLogger().severe("MySQL DATABASE CAN NOT BE REACHED.");
@@ -72,16 +72,10 @@ public class MySQLData extends SQLData {
} }
private void openConnection() throws SQLException { 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 // Connection isn't null AND Connection isn't closed :: return
try { try {
if (isConnectionOpen()) { if (isConnectionOpen()) return;
return; if (connection != null) close(); // Close connection if still active
} else if (connection != null) close(); // Close connection if still active
} catch (RuntimeException e) { } catch (RuntimeException e) {
e.printStackTrace(); // If isConnectionOpen() throws error e.printStackTrace(); // If isConnectionOpen() throws error
} }
@@ -90,17 +84,15 @@ public class MySQLData extends SQLData {
try { try {
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, setupProperties()); connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, setupProperties());
} catch (SQLException e) { } catch (SQLException | ClassNotFoundException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} }
} }
public void close() { public void close() {
Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> { Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
try { try {
if (connection == null) throw new NullPointerException("Connection is null"); if (connection == null) throw new IllegalStateException("Connection is null");
connection.close(); connection.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
@@ -111,13 +103,12 @@ public class MySQLData extends SQLData {
@NotNull @NotNull
private Properties setupProperties() { private Properties setupProperties() {
Properties props = new Properties(); Properties props = new Properties();
props.put("user", user); props.setProperty("user", user);
props.put("password", password); props.setProperty("password", password);
props.put("autoReconnect", "true");
return props; return props;
} }
private boolean isConnectionOpen() throws RuntimeException { private boolean isConnectionOpen() {
try { try {
return connection != null && !connection.isClosed(); return connection != null && !connection.isClosed();
} catch (SQLException e) { } catch (SQLException e) {
@@ -139,9 +130,9 @@ public class MySQLData extends SQLData {
} }
try { try {
if (connection == null) throw new NullPointerException("Connection is null"); if (connection == null) throw new IllegalStateException("Connection is null");
ps = connection.prepareStatement(query); ps = connection.prepareStatement(query);
} catch (SQLException | NullPointerException e) { } catch (SQLException | IllegalStateException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -60,11 +60,6 @@ public class SQLiteData extends SQLData {
} }
private void openConnection() throws SQLException { 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; if (connection != null && !connection.isClosed()) return;
// Close Connection if still active // Close Connection if still active
@@ -84,9 +79,12 @@ public class SQLiteData extends SQLData {
@Override @Override
public PreparedStatement preparedStatement(String query) { public PreparedStatement preparedStatement(String query) {
PreparedStatement ps = null; PreparedStatement ps = null;
if (!isConnectionOpen()) MessagesUtil.sendDebugMessages("Connection is not open");
try { try {
if (!isConnectionOpen()) {
MessagesUtil.sendDebugMessages("Connection is not open");
openConnection();
}
ps = connection.prepareStatement(query); ps = connection.prepareStatement(query);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();