From 03620a2a86ed70fa24caa85a523db528c11b2c90 Mon Sep 17 00:00:00 2001 From: poopki Date: Mon, 11 Jul 2022 23:03:08 +0900 Subject: [PATCH] =?UTF-8?q?SQL=20=EB=B2=84=EA=B7=B8=20=ED=95=B4=EA=B2=B0?= =?UTF-8?q?=20=EC=99=84=EB=A3=8C=20=EB=B0=8F=20DB=20=EA=B4=80=EB=A6=AC=20B?= =?UTF-8?q?ase=20=EC=BD=94=EB=93=9C=20=EA=B5=AC=ED=98=84=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DB/DBUpdate.java | 102 ++++++++++++++++++++++++++++++++++++++++------- UserInfo.java | 18 ++++++++- User_DB.java | 17 ++++++-- 3 files changed, 119 insertions(+), 18 deletions(-) diff --git a/DB/DBUpdate.java b/DB/DBUpdate.java index 5ee87cb..71c167b 100644 --- a/DB/DBUpdate.java +++ b/DB/DBUpdate.java @@ -1,5 +1,6 @@ package org.poopki.duckdns.user_db.DB; +import org.poopki.duckdns.user_db.UserInfo; import org.poopki.duckdns.user_db.UserInfoArray; import java.sql.*; @@ -19,7 +20,7 @@ public class DBUpdate { m_InfoArrayInstance = m_UIA; } - public static void UpdateDB(){ + public static void InitDB() throws SQLException { Connection conn = null; Statement stmt = null; @@ -30,23 +31,18 @@ public class DBUpdate { System.out.println("Connection success"); stmt = conn.createStatement(); StringBuilder sb = new StringBuilder(); - /* String sql = sb.append("create table if not exists UserInfo(") + String sql = sb.append("create table if not exists UserInfo(") .append("UUID varchar(36),") - .append("Nation int(8)") - .append("Occupation TINYINT,") - .append("Squad int,") - .append("Farm int,") - .append("Account int,") + .append("Nation int(8),") //public + .append("Squad int(8),") //public + .append("Farm int(8)") //public + /* + .append("Occupation int(8),") //private + .append("Account int(8),") //private + .append("Status int(8)") //private*/ .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(); - String sql = sb.append("insert into UserInfo values ('test', 0)").toString(); - stmt.execute(sql); } - } catch (ClassNotFoundException e) { e.printStackTrace(); } @@ -55,8 +51,86 @@ public class DBUpdate { System.err.println("에러 내용 :" + e.getMessage()); System.out.println("DB Connection fail"); } + finally { + if( conn != null && !conn.isClosed()){ + conn.close(); + } + } + } + public static void LoadDB() throws SQLException { + 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(); + String sql = "SELECT * FROM UserInfo"; + ResultSet rs = stmt.executeQuery(sql); + while(rs.next()){ + m_InfoArrayInstance.m_UserInfoArray.put(UUID.fromString(rs.getString(1)), new UserInfo(UUID.fromString(rs.getString(1)), + Integer.parseInt(rs.getString(2)), + Integer.parseInt(rs.getString(3)), + Integer.parseInt(rs.getString(4)))); + } + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + catch (SQLException e) { + // TODO Auto-generated catch block + System.err.println("에러 내용 :" + e.getMessage()); + System.out.println("DB Connection fail"); + }finally{ + if(conn != null && !conn.isClosed()){ + conn.close(); + } + } } + public void UpdateDB() throws SQLException { + Connection conn = null; + PreparedStatement pstmt = null; + + try{ + Class.forName("org.mariadb.jdbc.Driver"); + conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); + + for (UUID key : m_InfoArrayInstance.m_UserInfoArray.keySet()) { + //sql = sb.append("insert into UserInfo values('"+key+"',"+0+","+0+","+0+","+0+","+0+")").toString(); + //String sql = sb.append("insert into UserInfo values ('test', 0)").toString(); + //stmt.execute(sql); + String sql = "INSERT INTO UserInfo VALUES (?,?,?,?)"; + pstmt = conn.prepareStatement(sql); + //pstmt.setString(1, key.toString()); + pstmt.setString(1, String.valueOf(m_InfoArrayInstance.m_UserInfoArray.get(key).getUserUUID())); + pstmt.setString(2, String.valueOf(11)); + pstmt.setString(3, String.valueOf(12)); + pstmt.setString(4, String.valueOf(33)); + + int cnt = pstmt.executeUpdate(); + if( cnt == 0 ){ + System.out.println("데이터 입력 실패"); + } + else{ + System.out.println("데이터 입력 성공"); + } + + } + } + catch(ClassNotFoundException e){ + e.printStackTrace(); + } + catch(SQLException e){ + System.err.println("에러 내용 :" + e.getMessage()); + System.out.println("DB Connection fail"); + } + finally { + if( conn != null && !conn.isClosed()){ + conn.close(); + } + } + } } \ No newline at end of file diff --git a/UserInfo.java b/UserInfo.java index 5f4d3b3..b419591 100644 --- a/UserInfo.java +++ b/UserInfo.java @@ -7,11 +7,25 @@ import java.util.UUID; public class UserInfo { private UUID m_UUID; private String m_Name; + private int m_Nation; + private int m_Squad; + private int m_Farm; UserInfo(Player p){ m_UUID = p.getUniqueId(); - m_Name = p.getName(); + m_Nation = 0; + m_Squad = 0; + m_Farm = 0; } + public UserInfo(UUID uuid, int Nation, int Squad, int Farm){ + m_UUID = uuid; + m_Nation = Nation; + m_Squad = Squad; + m_Farm = Farm; + } + public void JoinNation(int Nation) { m_Nation = Nation; } + public void JoinSquad(int Squad) { m_Squad = Squad; } + public void JoinFarm(int Farm) { m_Farm = Farm; } public String getUserName(){ return m_Name; @@ -20,4 +34,6 @@ public class UserInfo { public UUID getUserUUID(){ return m_UUID; } + + } diff --git a/User_DB.java b/User_DB.java index e0c231e..8e1d211 100644 --- a/User_DB.java +++ b/User_DB.java @@ -4,22 +4,33 @@ import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; import org.poopki.duckdns.user_db.DB.*; import java.sql.Connection; +import java.sql.SQLException; import java.sql.Statement; public final class User_DB extends JavaPlugin implements Listener { UserInfoArray m_InfoArrayInstance = new UserInfoArray(); - + DBUpdate Database_backup = new DBUpdate(m_InfoArrayInstance); @Override public void onEnable() { // Plugin startup logic + try { + Database_backup.InitDB(); + Database_backup.LoadDB(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + 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(); + try{ + Database_backup.UpdateDB(); + } catch (SQLException e){ + throw new RuntimeException(e); + } } }