package mineplex.serverstatifier; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; public class StatusHistoryRepository { private String _connectionString = "jdbc:mysql://sqlstats.mineplex.com:3306/ServerStats"; private String _userName = "root"; private String _password = "tAbechAk3wR7tuTh"; private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ServerStats (id INT NOT NULL AUTO_INCREMENT, serverGroup VARCHAR(256), updated LONG, players INT, maxPlayers INT, US BOOLEAN NOT NULL DEFAULT '1', joinableServers INT, PRIMARY KEY (id));"; private static String CREATE_BUNGEE_TABLE = "CREATE TABLE IF NOT EXISTS BungeeStats (id INT NOT NULL AUTO_INCREMENT, address VARCHAR(256), updated LONG, players INT, maxPlayers INT, US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id));"; private static String INSERT_SERVER_STATS = "INSERT INTO ServerStats (serverGroup, updated, players, maxPlayers, US, joinableServers) VALUES (?, now(), ?, ?, ?, ?);"; private static String INSERT_BUNGEE_STATS = "INSERT INTO BungeeStats (address, updated, players, maxPlayers, US) VALUES (?, now(), ?, ?, ?);"; public static Connection connection; public void initialize() { PreparedStatement preparedStatement = null; try { Class.forName("com.mysql.jdbc.Driver"); if (connection == null || connection.isClosed()) connection = DriverManager.getConnection(_connectionString, _userName, _password); // Create table preparedStatement = connection.prepareStatement(CREATE_TABLE); preparedStatement.execute(); } catch (Exception exception) { exception.printStackTrace(); } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } } preparedStatement = null; try { if (connection == null || connection.isClosed()) connection = DriverManager.getConnection(_connectionString, _userName, _password); // Create table preparedStatement = connection.prepareStatement(CREATE_BUNGEE_TABLE); preparedStatement.execute(); } catch (Exception exception) { exception.printStackTrace(); } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public void saveServerStats(HashMap> globalGroupStatusData) { PreparedStatement preparedStatement = null; try { if (connection == null || connection.isClosed()) connection = DriverManager.getConnection(_connectionString, _userName, _password); preparedStatement = connection.prepareStatement(INSERT_SERVER_STATS); for (Entry> serverGroupData : globalGroupStatusData.entrySet()) { for (Entry serverGroup : serverGroupData.getValue().entrySet()) { preparedStatement.setString(1, serverGroup.getKey()); preparedStatement.setInt(2, serverGroup.getValue().Players); preparedStatement.setInt(3, serverGroup.getValue().MaxPlayers); preparedStatement.setBoolean(4, serverGroupData.getKey()); preparedStatement.setInt(5, serverGroup.getValue().getJoinableCount()); preparedStatement.addBatch(); } } preparedStatement.executeBatch(); } catch (Exception exception) { exception.printStackTrace(); } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public void saveBungeeStats(List retrieveBungeeStatuses) { PreparedStatement preparedStatement = null; try { if (connection == null || connection.isClosed()) connection = DriverManager.getConnection(_connectionString, _userName, _password); preparedStatement = connection.prepareStatement(INSERT_BUNGEE_STATS); for (BungeeStatusData bungeeStatusData : retrieveBungeeStatuses) { preparedStatement.setString(1, bungeeStatusData.Address); preparedStatement.setInt(2, bungeeStatusData.Players); preparedStatement.setInt(3, bungeeStatusData.MaxPlayers); preparedStatement.setBoolean(4, bungeeStatusData.US); preparedStatement.addBatch(); } preparedStatement.executeBatch(); } catch (Exception exception) { exception.printStackTrace(); } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }