Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.godtitan.ot.util;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- import java.util.logging.Level;
- import de.godtitan.ot.main.Main;
- public class MySQL {
- private static MySQL instance;
- private String host, database, user, password;
- private int port;
- private Connection conn;
- public ExecutorService executor;
- private Main plugin;
- public MySQL(Main plugin, String host, String database, String user, String password, int port) {
- this.host = host;
- this.database = database;
- this.user = user;
- this.password = password;
- this.port = port;
- this.plugin = plugin;
- this.executor = Executors.newCachedThreadPool();
- MySQL.instance = this;
- }
- public void update(PreparedStatement stat) {
- if (this.isConnected()) {
- this.executor.execute(() -> this.queryUpdate(stat));
- }
- }
- public void update(String stat) {
- if (this.isConnected()) {
- this.executor.execute(() -> this.queryUpdate(stat));
- }
- }
- public ResultSet asnycquery(PreparedStatement stmt) {
- if (this.isConnected()) {
- Future<ResultSet> future = this.executor.submit(new Callable<ResultSet>() {
- @Override
- public ResultSet call() throws Exception {
- return query(stmt);
- }
- });
- try {
- return future.get();
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public ResultSet ASYNCquery(String statment) {
- if (this.isConnected()) {
- Future<ResultSet> future = this.executor.submit(new Callable<ResultSet>() {
- @Override
- public ResultSet call() throws Exception {
- return query(statment);
- }
- });
- try {
- return future.get();
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public PreparedStatement prepare(String query) {
- if (this.isConnected()) {
- try {
- return this.getConnection().prepareStatement(query);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public void queryUpdate(String query) {
- if (this.isConnected()) {
- try (PreparedStatement statment = this.conn.prepareStatement(query)) {
- this.queryUpdate(statment);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- public ResultSet query(String query) {
- if (this.isConnected()) {
- try {
- return this.query(this.conn.prepareStatement(query));
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public void queryUpdate(PreparedStatement stmt) {
- if (this.isConnected()) {
- try {
- stmt.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public ResultSet query(PreparedStatement stat) {
- if (this.isConnected()) {
- try {
- return stat.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public boolean isConnected() {
- try {
- if (this.conn == null || !this.conn.isValid(10) || this.conn.isClosed()) {
- return false;
- }
- } catch (SQLException e) {
- return false;
- }
- return true;
- }
- public void connect() {
- try {
- this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database,
- this.user, this.password);
- this.plugin.getLogger().log(Level.INFO, "Verbindung mit MySQL aufgebaut!");
- } catch (SQLException e) {
- this.plugin.getLogger().log(Level.WARNING, "Fehler beim Verbinden mit MySQL");
- }
- }
- public Connection getConnection() {
- if (this.isConnected()) {
- return conn;
- }
- return null;
- }
- public void closeConnection() {
- if (this.isConnected()) {
- try {
- this.conn.close();
- } catch (SQLException e) {
- } finally {
- this.conn = null;
- }
- }
- }
- public static MySQL getInstance() {
- return instance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement