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(); } 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..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 @@ -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; @@ -25,20 +26,22 @@ 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(1, event.getPlayer().getUniqueId().toString()); + statement.setString(2, event.getPlayer().getUsername()); + statement.setString(3, event.getPlayer().getUsername()); statement.executeUpdate(); + statement.close(); + connection.close(); } catch (SQLException throwables) { throwables.printStackTrace(); }