Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.ban.tammo.mysql;
- import net.md_5.bungee.api.ProxyServer;
- import net.md_5.bungee.api.chat.TextComponent;
- import java.sql.*;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- /**
- * Created by Tammo on 28.06.2017.
- */
- public class MySQL {
- private Connection con;
- public String host;
- public String database;
- public String username;
- public String password;
- private ExecutorService executor;
- public MySQL() {
- this.executor = Executors.newCachedThreadPool();
- }
- public void connect() {
- if (!this.isConnected()) {
- try {
- this.con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":3306/" + this.database + "?autoreconnect?=true", this.username, this.password);
- ProxyServer.getInstance().getConsole().sendMessage(new TextComponent("§aMYSQL | Verbunden!"));
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public void disconnect() {
- if (this.isConnected()) {
- try {
- this.con.close();
- ProxyServer.getInstance().getConsole().sendMessage(new TextComponent("§4MYSQL | Verbindung geschlossen!"));
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public boolean isConnected() {
- return this.con != null;
- }
- public void update(final String qry) {
- if (this.isConnected()) {
- this.executor.execute(() -> this.updateSync(qry));
- }
- }
- public ResultSet getResult(final String qry) {
- if (this.isConnected()) {
- final Future<ResultSet> future = this.executor.submit(new Callable<ResultSet>() {
- @Override
- public ResultSet call() throws Exception {
- return MySQL.this.getSyncResult(qry);
- }
- });
- try {
- return future.get();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public void updateSync(final String qry) {
- try {
- final PreparedStatement st = this.con.prepareStatement(qry);
- st.executeUpdate();
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public ResultSet getSyncResult(final String qry) {
- try {
- final PreparedStatement st = this.con.prepareStatement(qry);
- return st.executeQuery();
- }
- catch (SQLException e) {
- e.printStackTrace();
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement