Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.fallenbreak.coinsapi.database.container;
- import org.bukkit.Bukkit;
- import java.sql.*;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.function.Consumer;
- public class AsyncMySQL {
- private ExecutorService executor;
- private MySQL sql;
- public static boolean unconnected = false;
- public AsyncMySQL(final String host, final int port, final String user, final String password, final String database) {
- try {
- this.sql = new MySQL(host, port, user, password, database);
- this.executor = Executors.newCachedThreadPool();
- unconnected = false;
- } catch (final Exception e) {
- unconnected = true;
- System.out.println("Stopping server! Reason: MYSQL - Not connect");
- Bukkit.shutdown();
- }
- }
- public void update(final PreparedStatement statement) {
- this.executor.execute(() -> this.sql.queryUpdate(statement));
- }
- public void update(final String statement) {
- this.executor.execute(() -> this.sql.queryUpdate(statement));
- }
- // statement = PreparedStatement
- public void query(final PreparedStatement statement, final Consumer<ResultSet> consumer) {
- this.executor.execute(() -> {
- final ResultSet result = this.sql.query(statement);
- consumer.accept(result);
- });
- }
- // statement = String
- public void query(final String statement, final Consumer<ResultSet> consumer) {
- this.executor.execute(() -> {
- final ResultSet result = this.sql.query(statement);
- consumer.accept(result);
- });
- }
- public PreparedStatement prepare(final String query) {
- try {
- return this.sql.getConnection().prepareStatement(query);
- } catch (final SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public MySQL getMySQL() {
- return this.sql;
- }
- @SuppressWarnings({"WeakerAccess", "SqlNoDataSourceInspection"})
- public static class MySQL {
- private final String host;
- private final String user;
- private final String password;
- private final String database;
- private final int port;
- private Connection conn;
- public MySQL(final String host, final int port, final String user, final String password, final String database) throws Exception {
- this.host = host;
- this.port = port;
- this.user = user;
- this.password = password;
- this.database = database;
- this.openConnection();
- }
- public void queryUpdate(final String query) {
- this.checkConnection();
- try (final PreparedStatement statement = this.conn.prepareStatement(query)) {
- this.queryUpdate(statement);
- } catch (final SQLException e) {
- e.printStackTrace();
- }
- }
- public void queryUpdate(final PreparedStatement statement) {
- this.checkConnection();
- try {
- try {
- statement.executeUpdate();
- } catch (final SQLException e) {
- e.printStackTrace();
- }
- } finally {
- try {
- statement.close();
- } catch (final SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public ResultSet query(final String query) {
- this.checkConnection();
- try {
- return this.query(this.conn.prepareStatement(query));
- } catch (final SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public ResultSet query(final PreparedStatement statement) {
- this.checkConnection();
- try {
- return statement.executeQuery();
- } catch (final SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public void checkConnection() {
- try {
- if (this.conn == null || !this.conn.isValid(10) || this.conn.isClosed()) {
- this.openConnection();
- }
- } catch (final 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 Connection getConnection() {
- return this.conn;
- }
- public void closeConnection() {
- try {
- this.conn.close();
- } catch (final SQLException e) {
- e.printStackTrace();
- } finally {
- this.conn = null;
- }
- }
- public boolean isConnected() {
- final String CHECK_SQL_QUERY = "SELECT 1";
- boolean isConnected = false;
- try {
- final PreparedStatement statement = this.conn.prepareStatement(CHECK_SQL_QUERY);
- isConnected = true;
- } catch (final SQLException | NullPointerException ignored) {
- }
- return isConnected;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement