Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import net.md_5.bungee.api.ProxyServer;
- import tn.bungeemanager.ConfigLibrary.FileConfiguration;
- import tn.bungeemanager.Core;
- import java.sql.*;
- import java.util.HashMap;
- import java.util.concurrent.TimeUnit;
- import java.util.logging.Level;
- public class Database {
- private Core plugin;
- //private Connection connection;
- private enum ConnectionType {
- PUNISHMENT, PLAYER, SERVER, PERMISSIONS, GAMESTATS
- }
- private HashMap<ConnectionType, Connection> connections;
- public Database(Core plugin) {
- this.plugin = plugin;
- if (!init()) {
- plugin.log(Level.SEVERE, "MySQL - SQL Connection has failed (Check config)", "BungeeManager - Plugin has been disabled");
- ProxyServer.getInstance().getPluginManager().getPlugins().remove(plugin);
- }
- ProxyServer.getInstance().getScheduler().schedule(plugin, () -> {
- try {
- // //if (connection != null && !connection.isClosed()) {
- /// connection.createStatement().execute("/* ping */ SELECT 1");
- /// }
- for(Connection connection : connections.values()) {
- if (connection != null && !connection.isClosed()) {
- connection.createStatement().execute("/* ping */ SELECT 1");
- }
- }
- } catch (SQLException e) {
- // connection = getNewConnection();
- closeAll();
- for(ConnectionType type : ConnectionType.values()) {
- getNewConnection(type);
- }
- }
- }, 5, 5, TimeUnit.MINUTES);
- }
- private void close(Connection c) {
- try {
- c.close();
- } catch (SQLException e) {
- e.printStackTrace();
- plugin.log(Level.SEVERE, "MySQL - Failed to close connection ");
- }
- }
- public void closeAll() {
- for(Connection c : connections.values()) {
- close(c);
- }
- connections.clear();
- }
- /**
- * @return - Return connection to the SQL
- * Initialise the connection to the SQL
- */
- protected Connection getNewConnection(ConnectionType type) {
- FileConfiguration cf = plugin.getConfig();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://" + cf.getString("Database.Host") + ":" + cf.getString("Database.Port") + "/" + type.toString();
- return DriverManager.getConnection(url, cf.getString("Database.Username"), cf.getString("Database.Password"));
- } catch (Exception e) {
- e.printStackTrace();
- plugin.log(Level.SEVERE, "MySQL - SQL Connection has failed to create a new connection");
- return null;
- }
- }
- public boolean init() {
- //return checkConnection();
- return checkAllConnections();
- }
- private boolean checkAllConnections() {
- for(ConnectionType con : ConnectionType.values()) {
- try {
- if(connections.get(con) == null || connections.get(con).isClosed()) {
- Connection connection = getNewConnection(con);
- connections.put(con, connection);
- }
- if(connections.get(con) == null || connections.get(con).isClosed())
- return false;
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- }
- return true;
- }
- /* private boolean checkConnection() {
- try {
- if (connection == null || connection.isClosed()) {
- connection = getNewConnection();
- if (connection == null || connection.isClosed())
- return false;
- query("CREATE TABLE IF NOT EXISTS MCParkourTimes (ID INT(10) NOT NULL auto_increment, UUID VARCHAR(36) NOT NULL, ParkourName VARCHAR(20) NOT NULL, Time INT(10) NOT NULL, Name VARCHAR(16), PRIMARY KEY (ID))");
- query("CREATE TABLE IF NOT EXISTS MCPlayers (UUID VARCHAR(36), XP INT(10), Rank VARCHAR(30), ParkoursCompleted INT(10))");
- query("CREATE TABLE IF NOT EXISTS MCVis (UUID VARCHAR(36), Visibility BOOLEAN)");
- query("CREATE TABLE IF NOT EXISTS TN_Players (UUID VARCHAR(36), Name VARCHAR(16), Credits INT(10), XP INT(10), Level INT(10))");
- query("CREATE TABLE IF NOT EXISTS GM_Groups (Name VARCHAR(36), Prefix VARCHAR(30), Suffix VARCHAR(30), Weight INT(3))");
- query("CREATE TABLE IF NOT EXISTS GM_Inheritance (Parent VARCHAR(36), Child (36))");
- query("CREATE TABLE IF NOT EXISTS GM_Players (UUID VARCHAR(36), PrimaryGroup VARCHAR(36), SecondaryGroups VARCHAR(200))");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- plugin.log(Level.SEVERE, "MySQL - Failed to check connection");
- return false;
- }
- return true;
- }*/
- private boolean query(ConnectionType type, String sql) throws SQLException {
- //return connection.createStatement().execute(sql);
- return connections.get(type).createStatement().execute(sql);
- }
- /*public void close() {
- try {
- if (connection != null)
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }*/
- /**
- * Main Query
- */
- private synchronized ResultSet sqlQuery(String query, Connection connection) {
- Statement statement = null;
- try {
- statement = connection.createStatement();
- } catch ( SQLException e ) {
- e.printStackTrace();
- plugin.log(Level.SEVERE, "MySQL - Failed to create statement in sqlQuery ");
- }
- ResultSet result = null;
- try {
- assert statement != null;
- result = statement.executeQuery( query );
- } catch ( SQLException e ) {
- e.printStackTrace();
- plugin.log(Level.SEVERE, "MySQL - Failed to perform statement in sqlQuery ");
- }
- return result;
- }
- /**
- * Any query which does not return a ResultSet object. Such as : INSERT,
- * UPDATE, CREATE TABLE...
- *
- */
- public void standardQuery(ConnectionType type, String query ) {
- // standardQuery( query, connection );
- standardQuery(query, connections.get(type));
- }
- /**
- * Check whether a field/entry exists in a database.
- * @return Whether or not a result has been found in the query.
- */
- public boolean existanceQuery(ConnectionType type, String query ) {
- boolean check = false;
- try {
- //check = sqlQuery( query, connection ).next();
- check = sqlQuery(query, connections.get(type)).next();
- } catch (SQLException e) {
- e.printStackTrace();
- plugin.log(Level.SEVERE, "MySQL - Failed to perform existance query ");
- }
- return check;
- }
- /**
- * Any query which returns a ResultSet object. Such as : SELECT Remember to
- * close the ResultSet object after you are done with it to free up
- * resources immediately. ----- ResultSet set =
- * sqlQuery("SELECT * FROM sometable;"); set.doSomething(); set.close();
- * -----
- * @return ResultSet
- */
- public ResultSet sqlQuery(ConnectionType type, String query ) {
- ResultSet res;
- // res = sqlQuery( query, connection );
- res = sqlQuery(query, connections.get(type));
- return res;
- }
- /**
- * Check whether the table name exists.
- */
- private synchronized int standardQuery(String query, Connection connection) {
- Statement statement;
- int rowsUpdated = 0;
- try {
- statement = connection.createStatement();
- rowsUpdated = statement.executeUpdate( query );
- statement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- plugin.log(Level.SEVERE, "MySQL - Failed to perform Standard Query");
- }
- return rowsUpdated;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement