package org.poopki.duckdns.user_db.DB; import org.poopki.duckdns.user_db.*; import org.poopki.duckdns.user_db.Array.AccountArray; import org.poopki.duckdns.user_db.Array.FarmArray; import org.poopki.duckdns.user_db.Array.NationArray; import org.poopki.duckdns.user_db.Array.SquadArray; import java.sql.*; import java.util.ArrayList; import java.util.List; import java.util.UUID; public class DBUpdate { private final String DB_DRIVER_CLASS = "org.mariadb.jdbc.Driver"; private final String DB_URL = "jdbc:mariadb://poopki.duckdns.org:3307/mc_dev"; private final String DB_USERNAME = "mc_dev"; private final String DB_PASSWORD = "!Rkdalsrn1027"; private UserInfoArray m_InfoArrayInstance; private AccountArray m_AccountArray; private SquadArray m_SquadArray; private FarmArray m_FarmArray; private NationArray m_NationArray; UUID ZERO_UUID = new UUID(0,0); static int MAX_FARM_NUM = 13; public DBUpdate(UserInfoArray UIA, FarmArray FA, NationArray NA, SquadArray SA ,AccountArray AA){ m_InfoArrayInstance = UIA; m_AccountArray = AA; m_FarmArray = FA; m_NationArray = NA; m_SquadArray = SA; } public void InitDB() throws SQLException { // 플러그인 초기 사용 시 테이블 생성 Connection conn = null; Statement stmt; 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) PRIMARY KEY,") .append("Name varchar(15), ") .append("Nation varchar(36),") //public .append("Squad varchar(36),") //public .append("Farm varchar(36),") //public .append("Account int(10)") //public /* .append("Occupation int(8),") //private .append("Account int(8),") //private .append("Status int(8)") //private*/ .append(")").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"); } finally { if( conn != null && !conn.isClosed()){ conn.close(); } } } public void LoadDB() throws SQLException { //서버 초기 구동 시 DB 로드 Connection conn = null; Statement stmt; 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.putUserInfo(UUID.fromString(rs.getString(1)), rs.getString(2), UUID.fromString(rs.getString(3)), UUID.fromString(rs.getString(4)), UUID.fromString(rs.getString(5)), new UUID(0,0)); m_AccountArray.putAccountInfo(UUID.fromString(rs.getString(1)),Integer.parseInt(rs.getString(6))); } } 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(); } } 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 Farm"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ List m_MemberList = new ArrayList<>(); for(int index = 4; index m_MemberList = new ArrayList<>(); for(int index = 4; index m_MemberList = new ArrayList<>(); for(int index = 4; index<11; index++){ if(rs.getString(index) != null){ m_MemberList.add(index-4,UUID.fromString(rs.getString(index))); } } m_SquadArray.PutGroup(UUID.fromString(rs.getString(1)), rs.getString(2), m_MemberList); m_AccountArray.putAccountInfo(UUID.fromString(rs.getString(1)),Integer.parseInt(rs.getString(3))); } } 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 { // 서버 종료시 DB 업데이트 Connection conn = null; PreparedStatement pstmt; // Update UserINFO try{ Class.forName("org.mariadb.jdbc.Driver"); conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); for (UUID key : m_InfoArrayInstance.getKeySet()) { String sql = "REPLACE INTO UserInfo VALUES (?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, String.valueOf(key.toString())); pstmt.setString(2, String.valueOf(m_InfoArrayInstance.getUserName(key))); pstmt.setString(3, String.valueOf(m_InfoArrayInstance.getUserGroupUUID(key, "Nation"))); pstmt.setString(4, String.valueOf(m_InfoArrayInstance.getUserGroupUUID(key, "Squad"))); pstmt.setString(5, String.valueOf(m_InfoArrayInstance.getUserGroupUUID(key, "Farm"))); pstmt.setString(6, String.valueOf(m_AccountArray.getAccountInfo(key))); 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(); } } // Update Farm Info try{ Class.forName("org.mariadb.jdbc.Driver"); conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); for (UUID key : m_FarmArray.getKeySet()) { String sql = "REPLACE INTO Farm VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, String.valueOf(key.toString())); pstmt.setString(2, String.valueOf(m_FarmArray.GetGroupName(key))); pstmt.setString(3, String.valueOf(m_AccountArray.getAccountInfo(key))); int index = 4; for (UUID MemUUID : m_FarmArray.GetGroupMembersUUID(key)){ pstmt.setString(index, String.valueOf(MemUUID.toString())); index++; } for(; index<=MAX_FARM_NUM;index++){ pstmt.setString(index, null); } 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(); } } try{ Class.forName("org.mariadb.jdbc.Driver"); conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); for (UUID key: m_FarmArray.m_DeleteQueue){ String sql = "DELETE FROM Farm WHERE FARM_UUID=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, String.valueOf(key.toString())); int cnt = pstmt.executeUpdate(); m_FarmArray.m_DeleteQueue.remove(key); } } 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(); } } // Update nation Info try{ Class.forName("org.mariadb.jdbc.Driver"); conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); for (UUID key: m_NationArray.m_DeleteQueue){ String sql = "DELETE FROM Nation WHERE NATION_UUID (?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, String.valueOf(key.toString())); int cnt = pstmt.executeUpdate(); m_NationArray.m_DeleteQueue.remove(key); } for (UUID key : m_NationArray.getKeySet()) { String sql = "REPLACE INTO Nation VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, String.valueOf(key.toString())); pstmt.setString(2, String.valueOf(m_NationArray.GetGroupName(key))); pstmt.setString(3, String.valueOf(m_AccountArray.getAccountInfo(key))); int index = 4; for (UUID MemUUID : m_NationArray.GetGroupMembersUUID(key)){ pstmt.setString(index, String.valueOf(MemUUID.toString())); index++; } for(; index<=MAX_FARM_NUM;index++){ pstmt.setString(index, null); } 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(); } } try{ Class.forName("org.mariadb.jdbc.Driver"); conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); for (UUID key: m_NationArray.m_DeleteQueue){ String sql = "DELETE FROM Nation WHERE NATION_UUID=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, String.valueOf(key.toString())); int cnt = pstmt.executeUpdate(); m_NationArray.m_DeleteQueue.remove(key); } } 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(); } } try{ Class.forName("org.mariadb.jdbc.Driver"); conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); for (UUID key : m_SquadArray.getKeySet()) { String sql = "REPLACE INTO Squad VALUES (?,?,?,?,?,?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, String.valueOf(key.toString())); pstmt.setString(2, String.valueOf(m_SquadArray.GetGroupName(key))); pstmt.setString(3, String.valueOf(m_AccountArray.getAccountInfo(key))); int index = 4; for (UUID MemUUID : m_SquadArray.GetGroupMembersUUID(key)){ pstmt.setString(index, String.valueOf(MemUUID.toString())); index++; } for(; index<=11;index++){ pstmt.setString(index, null); } 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(); } } try{ Class.forName("org.mariadb.jdbc.Driver"); conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); for (UUID key: m_SquadArray.m_DeleteQueue){ String sql = "DELETE FROM Squad WHERE FARM_UUID=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, String.valueOf(key.toString())); int cnt = pstmt.executeUpdate(); m_SquadArray.m_DeleteQueue.remove(key); } } 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(); } } } }