diff --git a/src/main/java/vip/fubuki/playersync/PlayerSync.java b/src/main/java/vip/fubuki/playersync/PlayerSync.java index 669fa7a..6d17f31 100644 --- a/src/main/java/vip/fubuki/playersync/PlayerSync.java +++ b/src/main/java/vip/fubuki/playersync/PlayerSync.java @@ -41,11 +41,24 @@ public class PlayerSync @SubscribeEvent public void onServerStarting(ServerStartingEvent event) throws SQLException { - JDBCsetUp.executeUpdate("CREATE DATABASE IF NOT EXISTS "+JdbcConfig.DATABASE_NAME.get(),1); + JDBCsetUp.executeUpdate("CREATE DATABASE IF NOT EXISTS `playersync`",1); - JDBCsetUp.executeUpdate("CREATE TABLE IF NOT EXISTS player_data (uuid CHAR(36) NOT NULL," + - "inventory MEDIUMBLOB,armor BLOB,advancements BLOB,enderchest MEDIUMBLOB,effects BLOB," + - "xp int,food_level int,score int,health int,online boolean, last_server int, PRIMARY KEY (uuid))"); + JDBCsetUp.executeUpdate(""" + CREATE TABLE IF NOT EXISTS `player_data` ( + `uuid` char(36) NOT NULL, + `inventory` mediumblob, + `armor` blob, + `advancements` blob, + `enderchest` mediumblob, + `effects` blob, + `xp` int DEFAULT NULL, + `food_level` int DEFAULT NULL, + `score` int DEFAULT NULL, + `health` int DEFAULT NULL, + `online` tinyint(1) DEFAULT NULL, + `last_server` int DEFAULT NULL, + PRIMARY KEY (`uuid`) + );"""); JDBCsetUp.executeUpdate("CREATE TABLE IF NOT EXISTS chat (player CHAR(36) NOT NULL,message TEXT," + "timestamp BIGINT)"); JDBCsetUp.executeUpdate(""" diff --git a/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java b/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java index 5ea63df..3408b14 100644 --- a/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java +++ b/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java @@ -11,7 +11,6 @@ import java.util.Random; public class JdbcConfig { public static ForgeConfigSpec COMMON_CONFIG; public static ForgeConfigSpec.ConfigValue HOST; - public static ForgeConfigSpec.ConfigValue DATABASE_NAME; public static ForgeConfigSpec.IntValue PORT; public static ForgeConfigSpec.ConfigValue USERNAME; public static ForgeConfigSpec.ConfigValue PASSWORD; @@ -26,7 +25,6 @@ public class JdbcConfig { ForgeConfigSpec.Builder COMMON_BUILDER = new ForgeConfigSpec.Builder(); COMMON_BUILDER.comment("General settings").push("general"); HOST=COMMON_BUILDER.comment("The host of the database").define("host", "localhost"); - DATABASE_NAME= COMMON_BUILDER.comment("Database name").define("database_name", "playersync"); PORT = COMMON_BUILDER.comment("database port").defineInRange("db_port", 3306, 0, 65535); USE_SSL = COMMON_BUILDER.comment("whether use SSL").define("use_ssl", false); USERNAME = COMMON_BUILDER.comment("username").define("user_name", "root"); diff --git a/src/main/java/vip/fubuki/playersync/util/JDBCsetUp.java b/src/main/java/vip/fubuki/playersync/util/JDBCsetUp.java index 4626e7b..b6b6300 100644 --- a/src/main/java/vip/fubuki/playersync/util/JDBCsetUp.java +++ b/src/main/java/vip/fubuki/playersync/util/JDBCsetUp.java @@ -14,25 +14,28 @@ public class JDBCsetUp { public static QueryResult executeQuery(String sql) throws SQLException{ Connection connection = getConnection(); + PreparedStatement useStatement = connection.prepareStatement("USE `playersync`"); + useStatement.executeUpdate(); - PreparedStatement useStatement = connection.prepareStatement("USE " + JdbcConfig.DATABASE_NAME.get()); - useStatement.executeUpdate(); - - PreparedStatement queryStatement = connection.prepareStatement(sql); - ResultSet resultSet =queryStatement.executeQuery(); + PreparedStatement queryStatement = connection.prepareStatement(sql); + ResultSet resultSet = queryStatement.executeQuery(); return new QueryResult(connection,resultSet); } public static int executeUpdate(String sql) throws SQLException{ - return executeUpdate(sql,0); - } - - public static int executeUpdate(String sql,int init) throws SQLException{ try (Connection connection = getConnection()) { - if(init==0){ - sql="USE " + JdbcConfig.DATABASE_NAME.get() +";" + sql; + PreparedStatement useStatement = connection.prepareStatement("USE `playersync`"); + useStatement.executeUpdate(); + + try (PreparedStatement updateStatement = connection.prepareStatement(sql)) { + return updateStatement.executeUpdate(); } + } + } + + public static int executeUpdate(String sql,int i) throws SQLException{ + try (Connection connection = getConnection()) { try (PreparedStatement updateStatement = connection.prepareStatement(sql)) { return updateStatement.executeUpdate();