Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.D4rkP1ka.BKM;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- import java.util.function.Consumer;
- import java.util.logging.Logger;
- import net.md_5.bungee.BungeeCord;
- import net.md_5.bungee.api.plugin.Plugin;
- public class AsyncMySQL {
- private ExecutorService executor;
- private Plugin plugin;
- private MySQL sql;
- public AsyncMySQL(Plugin plugin, String host, int port, String user, String password, String database) {
- try {
- sql = new MySQL(host, port, user, password, database);
- executor = Executors.newCachedThreadPool();
- this.plugin = plugin;
- Logger.getLogger("").info("MySQL > Connected.");
- } catch (Exception e) {
- Logger.getLogger("").info("Error. Couldnt connect to your MySQL-DB.");
- Logger.getLogger("").info("Der Server stoppt nun.");
- BungeeCord.getInstance().stop();
- return;
- }
- }
- public void update(PreparedStatement statement) {
- executor.execute(() -> sql.queryUpdate(statement));
- }
- public void update(String statement) {
- executor.execute(() -> sql.queryUpdate(statement));
- }
- public void query(PreparedStatement statement, Consumer<ResultSet> consumer) {
- executor.execute(() -> {
- ResultSet result = sql.query(statement);
- BungeeCord.getInstance().getScheduler().runAsync(plugin, () -> consumer.accept(result));
- });
- }
- public void query(String statement, Consumer<ResultSet> consumer) {
- executor.execute(() -> {
- ResultSet result = sql.query(statement);
- BungeeCord.getInstance().getScheduler().runAsync(plugin, () -> consumer.accept(result));
- });
- }
- public PreparedStatement prepare(String query) {
- try {
- return sql.getConnection().prepareStatement(query);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public MySQL getMySQL() {
- return sql;
- }
- public static class MySQL {
- private String host, user, password, database;
- private int port;
- private Connection conn;
- public MySQL(String host, int port, String user, String password, String database) throws Exception {
- this.host = host;
- this.port = port;
- this.user = user;
- this.password = password;
- this.database = database;
- this.openConnection();
- }
- public void queryUpdate(String query) {
- checkConnection();
- try (PreparedStatement statement = conn.prepareStatement(query)) {
- queryUpdate(statement);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void queryUpdate(PreparedStatement statement) {
- checkConnection();
- try {
- statement.executeUpdate();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- statement.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public ResultSet query(String query) {
- checkConnection();
- try {
- return query(conn.prepareStatement(query));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public ResultSet query(PreparedStatement statement) {
- checkConnection();
- try {
- return statement.executeQuery();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public Connection getConnection() {
- return this.conn;
- }
- public void checkConnection() {
- try {
- if (this.conn == null || !this.conn.isValid(10) || this.conn.isClosed()) openConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public Connection openConnection() throws Exception {
- Class.forName("com.mysql.jdbc.Driver");
- return this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database+"?autoReconnect=true", this.user, this.password);
- }
- public void closeConnection() {
- try {
- this.conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- this.conn = null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement