Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.cadedev.skywars.sql;
- import java.sql.*;
- import java.text.MessageFormat;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- public class SQL {
- private final String host;
- private final String user;
- private final String password;
- private final String database;
- private Connection connection;
- private final ExecutorService service = Executors.newCachedThreadPool();
- public SQL(final String host, final String database, final String user, final String password) {
- this.host = host;
- this.database = database;
- this.user = user;
- this.password = password;
- }
- public SQL startConnect() throws SQLException {
- final String url = MessageFormat.format("jdbc:mysql://{0}:{1}/{2}", this.host, String.valueOf(3306), this.database + "?autoReconnect=true");
- this.connection = DriverManager.getConnection(url, this.user, this.password);
- return this;
- }
- public SQL startConnect(final Runnable runnable) throws SQLException {
- final String url = MessageFormat.format("jdbc:mysql://{0}:{1}/{2}", this.host, String.valueOf(3306), this.database + "?autoReconnect=true");
- this.connection = DriverManager.getConnection(url, this.user, this.password);
- runnable.run();
- return this;
- }
- private boolean isConnected() {
- try {
- return this.connection != null || !this.connection.isClosed();
- } catch (SQLException exe) {
- exe.printStackTrace();
- }
- return false;
- }
- public void closeConnection() throws SQLException {
- if (!isConnected()) {
- return;
- }
- this.connection.close();
- this.service.shutdown();
- this.connection = null;
- }
- public void update(final PreparedStatement preparedStatement) {
- this.service.execute(() -> {
- try {
- this.syncUpdate(preparedStatement);
- } catch (SQLException exe) {
- exe.printStackTrace();
- }
- });
- }
- private void syncUpdate(final PreparedStatement statement) throws SQLException {
- this.checkConnection();
- statement.executeUpdate();
- statement.close();
- }
- public void checkConnection() throws SQLException {
- if (!isConnected()) {
- this.startConnect();
- }
- }
- public void startQuery(final PreparedStatement query, final Callback<ResultSet> callback, final Lock lock, final Condition condition) {
- this.service.execute(new Query(query, callback, condition, lock, this));
- }
- public void startQuery(final PreparedStatement query, final Callback<ResultSet> callback) {
- this.service.execute(new Query(query, callback, null, null, this));
- }
- public PreparedStatement preparedStatement(final String update) {
- try {
- return connection.prepareStatement(update);
- } catch (SQLException exe) {
- exe.printStackTrace();
- }
- return null;
- }
- public class Query implements Runnable {
- private final PreparedStatement query;
- private final Callback<ResultSet> callback;
- private final Condition condition;
- private final Lock lock;
- private SQL mysql;
- public Query(final PreparedStatement query, final Callback<ResultSet> callback, final Condition condition, final Lock lock, final SQL mysql) {
- this.query = query;
- this.callback = callback;
- this.condition = condition;
- this.lock = lock;
- this.mysql = mysql;
- }
- public void run() {
- try {
- this.mysql.checkConnection();
- } catch (SQLException exe) {
- this.callback.onFaillure(exe.getCause());
- }
- final PreparedStatement preparedStatement = this.query;
- try {
- final ResultSet resultset = preparedStatement.executeQuery();
- if (resultset == null) {
- this.callback.onFaillure(new NullPointerException());
- } else {
- this.callback.onSuccess(resultset);
- if (this.condition != null || this.lock != null) {
- this.lock.lock();
- this.condition.signal();
- this.lock.unlock();
- }
- }
- } catch (SQLException exe) {
- this.callback.onFaillure(exe.getCause());
- } finally {
- if (preparedStatement != null) {
- try {
- preparedStatement.close();
- } catch (SQLException exe) {
- this.callback.onFaillure(exe.getCause());
- }
- }
- }
- }
- }
- public interface Callback<T> {
- void onSuccess(final T resultSet);
- void onFaillure(final Throwable couse);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement