package org.poopki.duckdns.user_db.DB; import org.poopki.duckdns.user_db.AccountArray; import org.poopki.duckdns.user_db.UserInfo; 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; public DBUpdate(UserInfoArray m_UIA){ m_InfoArrayInstance = m_UIA; } 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("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(AccountArray m_AccountArrayInstance) 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(); 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)), m_AccountArrayInstance.putAccountInfo( UUID.fromString( rs.getString(1)), Integer.parseInt(rs.getString(5))))); } } 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; 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 = "REPLACE INTO UserInfo VALUES (?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); //pstmt.setString(1, key.toString()); UserInfo temp = m_InfoArrayInstance.m_UserInfoArray.get(key); pstmt.setString(1, String.valueOf(key.toString())); pstmt.setString(2, String.valueOf(11)); pstmt.setString(3, String.valueOf(23)); pstmt.setString(4, String.valueOf(56)); pstmt.setString(5, String.valueOf(temp.getBalance())); 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(); } } } }