Fixed #12
This commit is contained in:
parent
f3a3bb9be0
commit
5eb33c5928
|
|
@ -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("""
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ import java.util.Random;
|
|||
public class JdbcConfig {
|
||||
public static ForgeConfigSpec COMMON_CONFIG;
|
||||
public static ForgeConfigSpec.ConfigValue<String> HOST;
|
||||
public static ForgeConfigSpec.ConfigValue<String> DATABASE_NAME;
|
||||
public static ForgeConfigSpec.IntValue PORT;
|
||||
public static ForgeConfigSpec.ConfigValue<String> USERNAME;
|
||||
public static ForgeConfigSpec.ConfigValue<String> 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");
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user