Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. package de.ban.tammo.mysql;
  2.  
  3. import net.md_5.bungee.api.ProxyServer;
  4. import net.md_5.bungee.api.chat.TextComponent;
  5.  
  6. import java.sql.*;
  7. import java.util.concurrent.Callable;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10. import java.util.concurrent.Future;
  11.  
  12. /**
  13.  * Created by Tammo on 28.06.2017.
  14.  */
  15. public class MySQL {
  16.  
  17.     private Connection con;
  18.  
  19.     public String host;
  20.     public String database;
  21.     public String username;
  22.     public String password;
  23.  
  24.     private ExecutorService executor;
  25.  
  26.     public MySQL() {
  27.         this.executor = Executors.newCachedThreadPool();
  28.     }
  29.  
  30.     public void connect() {
  31.         if (!this.isConnected()) {
  32.             try {
  33.                 this.con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":3306/" + this.database + "?autoreconnect?=true", this.username, this.password);
  34.                 ProxyServer.getInstance().getConsole().sendMessage(new TextComponent("§aMYSQL | Verbunden!"));
  35.             }
  36.             catch (SQLException e) {
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.     }
  41.  
  42.     public void disconnect() {
  43.         if (this.isConnected()) {
  44.             try {
  45.                 this.con.close();
  46.                 ProxyServer.getInstance().getConsole().sendMessage(new TextComponent("§4MYSQL | Verbindung geschlossen!"));
  47.             }
  48.             catch (SQLException e) {
  49.                 e.printStackTrace();
  50.             }
  51.         }
  52.     }
  53.  
  54.     public boolean isConnected() {
  55.         return this.con != null;
  56.     }
  57.  
  58.     public void update(final String qry) {
  59.         if (this.isConnected()) {
  60.             this.executor.execute(() -> this.updateSync(qry));
  61.         }
  62.     }
  63.  
  64.     public ResultSet getResult(final String qry) {
  65.         if (this.isConnected()) {
  66.             final Future<ResultSet> future = this.executor.submit(new Callable<ResultSet>() {
  67.                 @Override
  68.                 public ResultSet call() throws Exception {
  69.                     return MySQL.this.getSyncResult(qry);
  70.                 }
  71.             });
  72.             try {
  73.                 return future.get();
  74.             }
  75.             catch (Exception e) {
  76.                 e.printStackTrace();
  77.             }
  78.         }
  79.         return null;
  80.     }
  81.  
  82.     public void updateSync(final String qry) {
  83.         try {
  84.             final PreparedStatement st = this.con.prepareStatement(qry);
  85.             st.executeUpdate();
  86.         }
  87.         catch (SQLException e) {
  88.             e.printStackTrace();
  89.         }
  90.     }
  91.  
  92.     public ResultSet getSyncResult(final String qry) {
  93.         try {
  94.             final PreparedStatement st = this.con.prepareStatement(qry);
  95.             return st.executeQuery();
  96.         }
  97.         catch (SQLException e) {
  98.             e.printStackTrace();
  99.             return null;
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement