You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
11 KiB

package org.poopki.duckdns.user_db.DB;
import org.poopki.duckdns.user_db.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
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;
private static FarmArray m_FarmArray;
private static NationArray m_NationArray;
UUID ZERO_UUID = new UUID(0,0);
static int MAX_FARM_NUM = 13;
public DBUpdate(UserInfoArray UIA, AccountArray AA, FarmArray FA, NationArray NA){
m_InfoArrayInstance = UIA;
m_AccountArray = AA;
m_FarmArray = FA;
m_NationArray = NA;
}
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 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 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();
}
}
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<UUID> m_MemberList = new ArrayList<>();
for(int index = 4; index<MAX_FARM_NUM; index++){
m_MemberList.add(index-4,UUID.fromString(rs.getString(index)));
}
m_FarmArray.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();
}
}
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 Nation";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
List<UUID> m_MemberList = new ArrayList<>();
for(int index = 4; index<MAX_FARM_NUM; index++){
m_MemberList.add(index-4,UUID.fromString(rs.getString(index)));
}
m_NationArray.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, String.valueOf(ZERO_UUID));
}
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 nation Info
try{
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
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, String.valueOf(ZERO_UUID));
}
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();
}
}
}
}