From c16c08dfce3e8eba2162f150e637d90c49d5112b Mon Sep 17 00:00:00 2001 From: Starky Date: Thu, 26 May 2022 01:16:31 -0400 Subject: [PATCH 1/3] Properly closes table statement and connection --- .../java/com/grubnest/game/core/databasehandler/MySQL.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java b/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java index f946403..d6a526f 100644 --- a/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java +++ b/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java @@ -1,5 +1,6 @@ package com.grubnest.game.core.databasehandler; +import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -30,13 +31,16 @@ public void onDisable() { public void createTables() { try { - PreparedStatement statement = getConnection().prepareStatement(""" + Connection connection = getConnection(); + PreparedStatement statement = connection.prepareStatement(""" CREATE TABLE IF NOT EXISTS `player` ( uuid varchar(36) PRIMARY KEY, username varchar(16) ) """); statement.executeUpdate(); + statement.close(); + connection.close(); } catch (SQLException e) { e.printStackTrace(); } From 5d44663b3b368e7413f8d3e1c8e139edb8afbe04 Mon Sep 17 00:00:00 2001 From: Starky Date: Thu, 26 May 2022 01:19:16 -0400 Subject: [PATCH 2/3] Properly closes update statement and connection --- .../velocity/events/CoreEventListener.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java b/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java index cf14e79..87ef15a 100644 --- a/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java +++ b/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java @@ -5,6 +5,7 @@ import com.velocitypowered.api.event.player.ServerConnectedEvent; import net.kyori.adventure.text.Component; +import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -26,19 +27,23 @@ public class CoreEventListener { @Subscribe public void onServerConnect(ServerConnectedEvent event) { VelocityPlugin.getInstance().getServer().sendMessage(Component.text("SERVER CONNECTION EVENT FIRED FOR: " + event.getPlayer().getUsername())); - String query = """ - INSERT INTO player - (uuid, username) - VALUES - ("%uuid%", "%username%") - ON DUPLICATE KEY UPDATE - username = "%username%"; - """; - query = query.replaceAll("%username%", event.getPlayer().getUsername()).replaceAll("%uuid%", event.getPlayer().getUniqueId().toString()); try { - PreparedStatement statement = VelocityPlugin.getInstance().getMySQL().getConnection().prepareStatement(query); + Connection connection = VelocityPlugin.getInstance().getMySQL().getConnection(); + PreparedStatement statement = connection.prepareStatement(""" + INSERT INTO player + (uuid, username) + VALUES + (?, ?) + ON DUPLICATE KEY UPDATE + username = ?; + """); + statement.setString(0, event.getPlayer().getUniqueId().toString()); + statement.setString(1, event.getPlayer().getUsername()); + statement.setString(2, event.getPlayer().getUsername()); statement.executeUpdate(); + statement.close(); + connection.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } From 82873eaa71c7b29fdd18ed0169866cf3d6b08d0b Mon Sep 17 00:00:00 2001 From: Starky Date: Thu, 26 May 2022 01:33:00 -0400 Subject: [PATCH 3/3] Corrected index of statement placeholders --- .../game/core/velocity/events/CoreEventListener.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java b/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java index 87ef15a..a8ab741 100644 --- a/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java +++ b/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java @@ -26,8 +26,6 @@ public class CoreEventListener { */ @Subscribe public void onServerConnect(ServerConnectedEvent event) { - VelocityPlugin.getInstance().getServer().sendMessage(Component.text("SERVER CONNECTION EVENT FIRED FOR: " + event.getPlayer().getUsername())); - try { Connection connection = VelocityPlugin.getInstance().getMySQL().getConnection(); PreparedStatement statement = connection.prepareStatement(""" @@ -38,9 +36,9 @@ public void onServerConnect(ServerConnectedEvent event) { ON DUPLICATE KEY UPDATE username = ?; """); - statement.setString(0, event.getPlayer().getUniqueId().toString()); - statement.setString(1, event.getPlayer().getUsername()); + statement.setString(1, event.getPlayer().getUniqueId().toString()); statement.setString(2, event.getPlayer().getUsername()); + statement.setString(3, event.getPlayer().getUsername()); statement.executeUpdate(); statement.close(); connection.close();