package org.poopki.duckdns.user_db.DB; import org.poopki.duckdns.user_db.AccountArray; import org.poopki.duckdns.user_db.UserInfoArray; import java.sql.*; 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; private static AccountArray m_AccountArray; public DBUpdate(UserInfoArray UIA, AccountArray AA){ m_InfoArrayInstance = UIA; m_AccountArray = AA; } public static 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 int(8),") //public .append("Squad int(8),") //public .append("Farm int(8),") //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 static 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(); } } } public void UpdateDB() throws SQLException { // 서버 종료시 DB 업데이트 Connection conn = null; PreparedStatement pstmt; 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(); } } } }