187 lines
4.7 KiB
Java
187 lines
4.7 KiB
Java
package me.paradoxpixel.themepark.database;
|
|
|
|
import me.paradoxpixel.themepark.ThemeParkPlugin;
|
|
import me.paradoxpixel.themepark.config.YamlConfig;
|
|
import org.bukkit.Bukkit;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class Database {
|
|
|
|
public Database(YamlConfig config) {
|
|
if(config == null) {
|
|
enabled = false;
|
|
return;
|
|
}
|
|
|
|
enabled = config.getConfig().getBoolean("mysql.enabled");
|
|
host = config.getConfig().getString("mysql.host");
|
|
port = config.getConfig().getInt("mysql.port");
|
|
database = config.getConfig().getString("mysql.database");
|
|
user = config.getConfig().getString("mysql.user");
|
|
password = config.getConfig().getString("mysql.password");
|
|
}
|
|
|
|
private boolean enabled;
|
|
private String host;
|
|
private int port;
|
|
private String database;
|
|
private String user;
|
|
private String password;
|
|
|
|
private Connection connection;
|
|
private int TaskID;
|
|
|
|
public boolean isEnabled() {
|
|
return enabled;
|
|
}
|
|
|
|
public void connect() {
|
|
try {
|
|
Class.forName("com.mysql.jdbc.Driver").newInstance();
|
|
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
|
|
|
|
startPool();
|
|
} catch(Exception e) {
|
|
enabled = false;
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void disconnect() {
|
|
if(connection == null)
|
|
return;
|
|
|
|
try {
|
|
connection.close();
|
|
connection = null;
|
|
} catch(Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private boolean connected() {
|
|
if(connection == null)
|
|
return false;
|
|
|
|
try {
|
|
return !connection.isClosed();
|
|
} catch(Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void startPool() {
|
|
TaskID = Bukkit.getScheduler().runTaskLater(ThemeParkPlugin.getInstance(), new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
disconnect();
|
|
TaskID = 0;
|
|
}
|
|
}, 1200L).getTaskId();
|
|
}
|
|
|
|
private void stopPool() {
|
|
if(TaskID == 0)
|
|
return;
|
|
|
|
Bukkit.getScheduler().cancelTask(TaskID);
|
|
TaskID = 0;
|
|
}
|
|
|
|
public boolean execute(String query, HashMap<Integer, Object> objects) {
|
|
if(query == null || query.isEmpty())
|
|
return false;
|
|
|
|
if(!enabled)
|
|
return false;
|
|
|
|
if(!connected())
|
|
connect();
|
|
|
|
stopPool();
|
|
try {
|
|
PreparedStatement statement = connection.prepareStatement(query);
|
|
if(objects != null)
|
|
for(Map.Entry<Integer, Object> entry : objects.entrySet())
|
|
statement.setObject(entry.getKey(), entry.getValue());
|
|
|
|
boolean b = statement.execute();
|
|
statement.close();
|
|
startPool();
|
|
|
|
return b;
|
|
} catch(Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
startPool();
|
|
return false;
|
|
}
|
|
|
|
public int executeUpdate(String query, HashMap<Integer, Object> objects) {
|
|
if(query == null || query.isEmpty())
|
|
return 0;
|
|
|
|
if(!enabled)
|
|
return 0;
|
|
|
|
if(!connected())
|
|
connect();
|
|
|
|
stopPool();
|
|
try {
|
|
PreparedStatement statement = connection.prepareStatement(query);
|
|
for(Map.Entry<Integer, Object> entry : objects.entrySet())
|
|
statement.setObject(entry.getKey(), entry.getValue());
|
|
|
|
int i = statement.executeUpdate();
|
|
statement.close();
|
|
startPool();
|
|
|
|
return i;
|
|
} catch(Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
startPool();
|
|
return 0;
|
|
}
|
|
|
|
public ResultSet executeQuery(String query, HashMap<Integer, Object> objects) {
|
|
if(query == null || query.isEmpty())
|
|
return null;
|
|
|
|
|
|
if(!enabled)
|
|
return null;
|
|
|
|
if(!connected())
|
|
connect();
|
|
|
|
stopPool();
|
|
try {
|
|
PreparedStatement statement = connection.prepareStatement(query);
|
|
for(Map.Entry<Integer, Object> entry : objects.entrySet())
|
|
statement.setObject(entry.getKey(), entry.getValue());
|
|
|
|
ResultSet result = statement.executeQuery();
|
|
statement.close();
|
|
startPool();
|
|
|
|
return result;
|
|
} catch(Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
startPool();
|
|
return null;
|
|
}
|
|
|
|
}
|