Skip to content

Commit

Permalink
Merge pull request #26 from Grubnest/hotfix-sql-closing
Browse files Browse the repository at this point in the history
Hotfix sql closing
  • Loading branch information
Wyzards authored May 26, 2022
2 parents a85a2db + 82873ea commit d820ccb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.grubnest.game.core.databasehandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
Expand Down

0 comments on commit d820ccb

Please sign in to comment.