-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL.java
More file actions
124 lines (112 loc) · 5.37 KB
/
MySQL.java
File metadata and controls
124 lines (112 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.github.pinont.singularitylib.api.utils;
import com.github.pinont.singularitylib.api.manager.ConfigManager;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import java.sql.Connection;
import static com.github.pinont.singularitylib.plugin.CorePlugin.sendConsoleMessage;
import static com.github.pinont.singularitylib.plugin.CorePlugin.sendDebugMessage;
/**
* Utility class for managing MySQL database connections.
* Provides functionality to connect to MySQL databases with automatic configuration setup.
*/
public class MySQL {
private Connection connection;
/**
* Default constructor for MySQL utility class.
*/
public MySQL() {
}
private void defaultConfigSetup(String configPath) {
ConfigManager configManager = new ConfigManager(configPath);
configManager.set("database.host", "localhost");
configManager.set("database.port", 3306);
configManager.set("database.databaseName", "database");
configManager.set("database.username", "root");
configManager.set("database.password", "password");
configManager.set("database.timezone", "UTC");
configManager.set("database.useSSL", "false");
configManager.saveConfig();
sendConsoleMessage(ChatColor.YELLOW + "[DB] Please set the database configuration in database.yml");
}
/**
* Gets a connection to the database using the default config file.
*
* @return the database connection, or null if connection failed
*/
public Connection getConnection() {
return getConnection("config.yml");
}
/**
* Gets a connection to the database using the specified config file.
*
* @param configPath the path to the configuration file
* @return the database connection, or null if connection failed
*/
public Connection getConnection(String configPath) {
ConfigManager configManager = new ConfigManager(configPath);
FileConfiguration config = configManager.getConfig();
boolean database =
config.getString("database.host") == null ||
config.getString("database.port") == null || config.getString("database.databaseName") == null ||
config.getString("database.username") == null || config.getString("database.password") == null ||
config.getString("database.timezone") == null || config.getString("database.useSSL") == null;
if (database) {
defaultConfigSetup(configPath);
return null;
}
String host = config.getString("database.host");
int port = config.getInt("database.port");
String dbName = config.getString("database.databaseName");
String username = config.getString("database.username");
String password = config.getString("database.password");
String timeZone = config.getString("database.timezone", "UTC");
String ssl = config.getString("database.useSSL", "false");
String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName + "?useSSL=" + ssl + "&serverTimezone=" + timeZone + "&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
sendDebugMessage(ChatColor.YELLOW + "[DB] Connecting to MySQL: host=" + host + ", port=" + port + ", dbName=" + dbName + ", user=" + username);
try {
// Check if database exists, if not, create it
String baseUrl = "jdbc:mysql://" + host + ":" + port + "/?useSSL=" + ssl + "&serverTimezone=" + timeZone;
try (Connection baseConn = java.sql.DriverManager.getConnection(baseUrl, username, password)) {
sendConsoleMessage(ChatColor.YELLOW + "[DB] Connected to MySQL server for database check.");
try (java.sql.Statement stmt = baseConn.createStatement()) {
stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS `" + dbName + "`");
sendDebugMessage(ChatColor.YELLOW + "[DB] Ensured database '" + dbName + "' exists.");
}
}
// Now connect to the actual database
if (connection == null || connection.isClosed()) {
connection = java.sql.DriverManager.getConnection(url, username, password);
sendDebugMessage(ChatColor.GREEN + "[DB] Connected to database '" + dbName + "'.");
}
} catch (Exception e) {
sendConsoleMessage(ChatColor.RED + "[DB] MySQL connection error:\n" + e.getMessage());
return null;
}
return connection;
}
/**
* Initializes the database connection using the specified config file.
*
* @param configPath the path to the configuration file
*/
public void init(String configPath) {
connection = getConnection(configPath);
if (connection == null) {
sendConsoleMessage(ChatColor.RED + "[DB] Failed to connect to the database. Please check your configuration in database.yml");
} else {
sendConsoleMessage(ChatColor.GREEN + "[DB] Successfully connected to the database.");
}
}
/**
* Closes the database connection if it exists.
*/
public void closeConnection() {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}