Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.craftersland.customenderchest.storage;
- import net.craftersland.customenderchest.*;
- import org.bukkit.*;
- import org.bukkit.plugin.*;
- import java.util.*;
- import java.sql.*;
- public class MysqlSetup
- {
- private Connection conn;
- private EnderChest enderchest;
- private boolean tablesChecked;
- public MysqlSetup(final EnderChest enderchest) {
- this.conn = null;
- this.tablesChecked = false;
- this.enderchest = enderchest;
- this.setupDatabase();
- this.updateTables();
- }
- public void setupDatabase() {
- this.connectToDatabase();
- this.databaseMaintenanceTask();
- }
- private void tableMaintenance(final long inactiveTime, final Connection conn, final String tableName) {
- PreparedStatement preparedStatement = null;
- try {
- final String sql = "DELETE FROM `" + tableName + "` WHERE `last_seen` < ?";
- preparedStatement = conn.prepareStatement(sql);
- preparedStatement.setString(1, String.valueOf(inactiveTime));
- preparedStatement.execute();
- }
- catch (Exception e) {
- e.printStackTrace();
- try {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- return;
- }
- finally {
- try {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- try {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- private void databaseMaintenanceTask() {
- if (this.enderchest.getConfigHandler().getBoolean("database.mysql.removeOldUsers.enabled")) {
- Bukkit.getScheduler().runTaskLaterAsynchronously((Plugin)this.enderchest, (Runnable)new Runnable() {
- @Override
- public void run() {
- if (MysqlSetup.this.conn != null) {
- final long inactivityDays = Long.parseLong(MysqlSetup.this.enderchest.getConfigHandler().getString("database.mysql.removeOldUsers.inactive"));
- final long inactivityMils = inactivityDays * 24L * 60L * 60L * 1000L;
- final long curentTime = System.currentTimeMillis();
- final long inactiveTime = curentTime - inactivityMils;
- EnderChest.log.info("Database maintenance task started...");
- MysqlSetup.this.tableMaintenance(inactiveTime, MysqlSetup.this.getConnection(), MysqlSetup.this.enderchest.getConfigHandler().getString("database.mysql.tableName"));
- EnderChest.log.info("Database maintenance complete!");
- }
- }
- }, 2000L);
- }
- }
- private void connectToDatabase() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- final Properties properties = new Properties();
- properties.setProperty("user", this.enderchest.getConfigHandler().getString("database.mysql.user"));
- properties.setProperty("password", this.enderchest.getConfigHandler().getString("database.mysql.password"));
- properties.setProperty("autoReconnect", "true");
- properties.setProperty("verifyServerCertificate", "false");
- properties.setProperty("useSSL", this.enderchest.getConfigHandler().getString("database.mysql.ssl"));
- properties.setProperty("requireSSL", this.enderchest.getConfigHandler().getString("database.mysql.ssl"));
- this.conn = DriverManager.getConnection("jdbc:mysql://" + this.enderchest.getConfigHandler().getString("database.mysql.host") + ":" + this.enderchest.getConfigHandler().getString("database.mysql.port") + "/" + this.enderchest.getConfigHandler().getString("database.mysql.databaseName"), properties);
- EnderChest.log.info("Database connection established!");
- if (!this.tablesChecked) {
- this.setupTables();
- }
- }
- catch (ClassNotFoundException e) {
- EnderChest.log.severe("Could not locate drivers for mysql! Error: " + e.getMessage());
- }
- catch (SQLException e2) {
- EnderChest.log.severe("Could not connect to mysql database! Error: " + e2.getMessage());
- }
- catch (Exception ex) {
- EnderChest.log.severe("Could not connect to mysql database! Error: " + ex.getMessage());
- ex.printStackTrace();
- }
- }
- public void setupTables() {
- if (this.conn != null) {
- PreparedStatement query1 = null;
- try {
- final String data = "CREATE TABLE IF NOT EXISTS `" + this.enderchest.getConfigHandler().getString("database.mysql.tableName") + "` (id int(10) AUTO_INCREMENT, player_uuid varchar(50) NOT NULL UNIQUE, player_name varchar(50) NOT NULL, enderchest_data LONGTEXT NOT NULL, size int(3) NOT NULL, last_seen varchar(30) NOT NULL, PRIMARY KEY(id));";
- query1 = this.conn.prepareStatement(data);
- query1.execute();
- this.tablesChecked = true;
- }
- catch (Exception e) {
- EnderChest.log.severe("Error creating tables! Error: " + e.getMessage());
- e.printStackTrace();
- try {
- if (query1 != null) {
- query1.close();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- return;
- }
- finally {
- try {
- if (query1 != null) {
- query1.close();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- try {
- if (query1 != null) {
- query1.close();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- public Connection getConnection() {
- this.checkConnection();
- return this.conn;
- }
- public void closeConnection() {
- try {
- EnderChest.log.info("Closing database connection...");
- if (this.conn != null) {
- this.conn.close();
- this.conn = null;
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private void checkConnection() {
- try {
- if (this.conn == null) {
- EnderChest.log.warning("Database connection failed. Reconnecting...");
- this.conn = null;
- this.connectToDatabase();
- }
- else if (!this.conn.isValid(3)) {
- EnderChest.log.warning("Database connection failed. Reconnecting...");
- this.conn = null;
- this.connectToDatabase();
- }
- else if (this.conn.isClosed()) {
- EnderChest.log.warning("Database connection failed. Reconnecting...");
- this.conn = null;
- this.connectToDatabase();
- }
- }
- catch (Exception e) {
- EnderChest.log.severe("Error re-connecting to the database! Error: " + e.getMessage());
- }
- }
- public boolean closeDatabase() {
- try {
- this.conn.close();
- this.conn = null;
- return true;
- }
- catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- }
- private void updateTables() {
- if (this.conn != null) {
- DatabaseMetaData md = null;
- ResultSet rs1 = null;
- ResultSet rs2 = null;
- PreparedStatement query1 = null;
- PreparedStatement query2 = null;
- try {
- md = this.conn.getMetaData();
- rs1 = md.getColumns(null, null, this.enderchest.getConfigHandler().getString("database.mysql.tableName"), "enderchest");
- if (rs1.next()) {
- final String data1 = "ALTER TABLE `" + this.enderchest.getConfigHandler().getString("database.mysql.tableName") + "` CHANGE COLUMN enderchest enderchest_data LONGTEXT NOT NULL;";
- query1 = this.conn.prepareStatement(data1);
- query1.execute();
- }
- else {
- rs2 = md.getColumns(null, null, this.enderchest.getConfigHandler().getString("database.mysql.tableName"), "enderchest_data");
- if (rs2.next() && rs2.getString("TYPE_NAME").matches("VARCHAR")) {
- final String data2 = "ALTER TABLE `" + this.enderchest.getConfigHandler().getString("database.mysql.tableName") + "` MODIFY enderchest_data LONGTEXT NOT NULL;";
- query2 = this.conn.prepareStatement(data2);
- query2.execute();
- }
- }
- }
- catch (Exception e) {
- EnderChest.log.warning("Error on table update! Error: " + e.getMessage());
- e.printStackTrace();
- try {
- if (query1 != null) {
- query1.close();
- }
- if (query2 != null) {
- query2.close();
- }
- if (rs1 != null) {
- rs1.close();
- }
- if (rs2 != null) {
- rs2.close();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- return;
- }
- finally {
- try {
- if (query1 != null) {
- query1.close();
- }
- if (query2 != null) {
- query2.close();
- }
- if (rs1 != null) {
- rs1.close();
- }
- if (rs2 != null) {
- rs2.close();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- try {
- if (query1 != null) {
- query1.close();
- }
- if (query2 != null) {
- query2.close();
- }
- if (rs1 != null) {
- rs1.close();
- }
- if (rs2 != null) {
- rs2.close();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement