SQL 버그 해결 완료 및 DB 관리 Base 코드 구현 완료

DB
poopki 3 years ago
parent 7b7bbada94
commit 03620a2a86
  1. 102
      DB/DBUpdate.java
  2. 18
      UserInfo.java
  3. 17
      User_DB.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();
}
}
}
}

@ -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;
}
}

@ -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);
}
}
}

Loading…
Cancel
Save