· 9 years ago · Aug 14, 2017, 07:58 PM
1import org.bukkit.configuration.file.YamlConfiguration;
2
3import java.io.File;
4import java.sql.*;
5
6public class DatabaseManager {
7 private String ip;
8 private String dbName;
9 private String usrName;
10 private String password;
11 private int port = 3306;
12 private Connection connection;
13 private boolean failedMySQL = false;
14
15 private static DatabaseManager instance = null;
16
17 public static DatabaseManager get() {
18 return instance == null ? instance = new DatabaseManager() : instance;
19 }
20
21 public void setup(){
22 File file = new File(Main.get().getDataFolder(), "MySQL.yml");
23 boolean createFile = !file.exists();
24
25 if (createFile) {
26 //TODO ne neue MySQL.yml file erstllen
27 failedMySQL = true;
28 }else{
29 YamlConfiguration mysqlFile = YamlConfiguration.loadConfiguration(file);
30 ip = mysqlFile.getString( "MySQL.IP", "Unknown");
31 dbName = mysqlFile.getString("MySQL.DB-Name", "Unknown");
32 usrName = mysqlFile.getString("MySQL.Username", "Unknown");
33 password = mysqlFile.getString("MySQL.Password", "Unknown");
34 port = mysqlFile.getInt("MySQL.Port", 3306);
35
36 connectMySQLServer();
37 }
38
39 //TODO exec statement: CREATE TABLE IF NOT EXISTS [...]
40 }
41
42 public void shutdown(){
43 try {
44 connection.close();
45 } catch (SQLException e) {
46 e.printStackTrace();
47 }
48 }
49
50 private void connectMySQLServer() {
51 try {
52 connection = DriverManager.getConnection("jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?verifyServerCertificate=false&useSSL=false&autoReconnect=true", usrName, password);
53 } catch (Exception exc) {
54 System.out.println("\n \n \nMySQL-Error\nCould not connect to MySQL-Server!\nCheck your MySQL.yml \n \n");
55 failedMySQL = true;
56 }
57 }
58
59 public void executeStatement(String sql, Object... parameters) {
60 executeStatement(sql, false, parameters);
61 }
62
63 public ResultSet executeResultStatement(String sql, Object... parameters) {
64 return executeStatement(sql, true, parameters);
65 }
66
67 private ResultSet executeStatement(String sql, boolean result, Object... parameters) {
68 try {
69 PreparedStatement statement = connection.prepareStatement(sql);
70
71 for (int i = 0; i < parameters.length; i++) {
72 Object obj = parameters[i];
73 if(obj instanceof Integer)
74 statement.setInt(i + 1, (Integer) obj);
75 else if(obj instanceof String)
76 statement.setString(i+1, (String) obj);
77 else if(obj instanceof Long)
78 statement.setLong(i+1, (Long) obj);
79 else
80 statement.setObject(i+1, obj);
81 }
82
83 if(result){
84 ResultSet resultSet = statement.executeQuery();
85 return resultSet;
86 }else{
87 statement.execute();
88 statement.close();
89 }
90 return null;
91 } catch (SQLException e) {
92 System.out.println("SQL -> "+sql);
93 e.printStackTrace();
94 return null;
95 }
96 }
97
98 public boolean isConnectionValid(int timeout){
99 try {
100 return connection.isValid(timeout);
101 } catch (SQLException e) {
102 e.printStackTrace();
103 return false;
104 }
105 }
106
107 public boolean isFailedMySQL() {
108 return failedMySQL;
109 }
110}