From e9298f7be0505a4663e045cb5f383864ca3ba1c9 Mon Sep 17 00:00:00 2001 From: poopki Date: Mon, 11 Jul 2022 00:02:48 +0900 Subject: [PATCH] Fix SQL Bug --- DB/DBUpdate.java | 61 ++++++++++++++++++++++++++++++++++++++++++++++ UserInfoArray.java | 2 +- UserJoin.java | 10 ++++---- User_DB.java | 7 +++--- 4 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 DB/DBUpdate.java diff --git a/DB/DBUpdate.java b/DB/DBUpdate.java new file mode 100644 index 0000000..e32d64d --- /dev/null +++ b/DB/DBUpdate.java @@ -0,0 +1,61 @@ +package org.poopki.duckdns.user_db.DB; + +import org.poopki.duckdns.user_db.UserInfoArray; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class DBUpdate { + private static final String DB_DRIVER_CLASS = "org.mariadb.jdbc.Driver"; + private static final String DB_URL = "jdbc:mariadb://poopki.duckdns.org:3307/mc_dev"; + private static final String DB_USERNAME = "mc_dev"; + private static final String DB_PASSWORD = "!Rkdalsrn1027"; + private static UserInfoArray m_InfoArrayInstance; + + public DBUpdate(UserInfoArray m_UIA){ + m_InfoArrayInstance = m_UIA; + } + + public static void UpdateDB(){ + Connection conn = null; + Statement stmt = null; + + try { + Class.forName("org.mariadb.jdbc.Driver"); + + conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); + System.out.println("Connection success"); + stmt = conn.createStatement(); + StringBuilder sb = new StringBuilder(); + String sql = sb.append("create table if not exists UserInfo(") + .append("UUID varchar(36)") +/* .append("Nation int,") + .append("Occupation TINYINT,") + .append("Squad int,") + .append("Farm int,") + .append("Account int,")*/ + .append(")").toString(); + stmt.execute(sql); + + for (UUID key : m_InfoArrayInstance.m_UserInfoArray.keySet()) { + //sql = sb.append("insert into UserInfo values('"+key+"',"+0+","+0+","+0+","+0+","+0+")").toString(); + sql = sb.append("insert into UserInfo (UUID) value ('"+key.toString()+ "')").toString(); + stmt.execute(sql); + } + } + catch (ClassNotFoundException e) { + e.printStackTrace(); + } + catch (SQLException e) { + // TODO Auto-generated catch block + System.err.println("에러 내용 :" + e.getMessage()); + System.out.println("DB Connection fail"); + } + + + } + +} \ No newline at end of file diff --git a/UserInfoArray.java b/UserInfoArray.java index b60e700..5183a00 100644 --- a/UserInfoArray.java +++ b/UserInfoArray.java @@ -7,7 +7,7 @@ import java.util.Map; import java.util.UUID; public class UserInfoArray { - static Map m_UserInfoArray = new HashMap(); + public static Map m_UserInfoArray = new HashMap(); public void getUserInfo(Player p) { UserInfo m_UserInfo = m_UserInfoArray.get(p.getUniqueId()); diff --git a/UserJoin.java b/UserJoin.java index fbaa205..3d322de 100644 --- a/UserJoin.java +++ b/UserJoin.java @@ -5,18 +5,18 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; public class UserJoin implements Listener { - private UserInfoArray m_InfoArray; + private UserInfoArray m_InfoArrayInstance; UserJoin(UserInfoArray m_UIA){ - m_InfoArray = m_UIA; + m_InfoArrayInstance = m_UIA; } @EventHandler public void PlayerLogin(PlayerJoinEvent e) { Player p = e.getPlayer(); - if(m_InfoArray.m_UserInfoArray.containsKey(p.getUniqueId())){ - m_InfoArray.getUserInfo(p); + if(m_InfoArrayInstance.m_UserInfoArray.containsKey(p.getUniqueId())){ + m_InfoArrayInstance.getUserInfo(p); } else{ - m_InfoArray.putUserInfo(p); + m_InfoArrayInstance.putUserInfo(p); } } } diff --git a/User_DB.java b/User_DB.java index 142c4b4..e0c231e 100644 --- a/User_DB.java +++ b/User_DB.java @@ -8,17 +8,18 @@ import java.sql.Statement; public final class User_DB extends JavaPlugin implements Listener { - UserInfoArray m_UIA = new UserInfoArray(); + UserInfoArray m_InfoArrayInstance = new UserInfoArray(); @Override public void onEnable() { // Plugin startup logic - Connection conn = new DBConnect().connectDB(); - getServer().getPluginManager().registerEvents(new UserJoin(m_UIA),this); + getServer().getPluginManager().registerEvents(new UserJoin(m_InfoArrayInstance),this); } @Override public void onDisable() { // Plugin shutdown logic + DBUpdate Database_backup = new DBUpdate(m_InfoArrayInstance); + Database_backup.UpdateDB(); } }