Changed database to not be static
This commit is contained in:
parent
34f83f20a1
commit
dec8b50317
|
|
@ -36,7 +36,7 @@ public class VillagerUI extends JavaPlugin {
|
||||||
WorthConfig.reload();
|
WorthConfig.reload();
|
||||||
if (!setupEconomy())
|
if (!setupEconomy())
|
||||||
return;
|
return;
|
||||||
Database.init();
|
Database.getDatabase().init();
|
||||||
Logger.info("--------------------------------------------------");
|
Logger.info("--------------------------------------------------");
|
||||||
Logger.info("Villager UI started");
|
Logger.info("Villager UI started");
|
||||||
Logger.info("--------------------------------------------------");
|
Logger.info("--------------------------------------------------");
|
||||||
|
|
|
||||||
|
|
@ -11,29 +11,56 @@ import java.sql.SQLException;
|
||||||
|
|
||||||
public class Database {
|
public class Database {
|
||||||
|
|
||||||
|
private static Database instance = null;
|
||||||
public static Connection connection = null;
|
public static Connection connection = null;
|
||||||
|
|
||||||
public static void init() { //Not static so we know for sure it loads on time
|
private Database() {
|
||||||
String url = "jdbc:" + Config.DRIVER +
|
|
||||||
"://" + Config.IP +
|
}
|
||||||
":" + Config.PORT +
|
|
||||||
"/" + Config.DATABASE_NAME +
|
public static Database getDatabase(){
|
||||||
"?autoReconnect=true&useSSL=false";
|
if (instance == null)
|
||||||
|
instance = new Database();
|
||||||
|
return (instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
try {
|
try {
|
||||||
connection = DriverManager.getConnection(url, Config.USERNAME, Config.PASSWORD);
|
openConnection();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Logger.severe("Connection to database failed!");
|
|
||||||
connection = null;
|
|
||||||
Logger.severe("Shutting down VillagerUI");
|
|
||||||
Bukkit.getPluginManager().disablePlugin(VillagerUI.getInstance());
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tables
|
// Tables
|
||||||
createUserPointsTable();
|
createUserPointsTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens the connection if it's not already open.
|
||||||
|
* @throws SQLException If it can't create the connection.
|
||||||
|
*/
|
||||||
|
private void openConnection() throws SQLException {
|
||||||
|
if (connection != null && !connection.isClosed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
|
if (connection != null && !connection.isClosed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
connection = DriverManager.getConnection(
|
||||||
|
"jdbc:mysql://" + Config.IP + ":" + Config.PORT + "/" + Config.DATABASE_NAME +
|
||||||
|
"?autoReconnect=true&useSSL=false",
|
||||||
|
Config.USERNAME, Config.PASSWORD);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void createUserPointsTable() {
|
private static void createUserPointsTable() {
|
||||||
try {
|
try {
|
||||||
String sql = "CREATE TABLE IF NOT EXISTS user_points(" +
|
String sql = "CREATE TABLE IF NOT EXISTS user_points(" +
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user