Advertisement
Guest User

Untitled

a guest
Sep 5th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. package net.exploit.statsapi.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.HashMap;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11. import java.util.function.Consumer;
  12.  
  13. import org.bukkit.Bukkit;
  14. import org.bukkit.plugin.Plugin;
  15.  
  16. import net.exploit.statsapi.main.StatsAPI;
  17. import net.exploit.statsapi.manager.LoginInfoManager;
  18.  
  19. public class MySQL {
  20.  
  21.     private Connection connection;
  22.  
  23.     private ExecutorService executor;
  24.     private Plugin plugin;
  25.  
  26.     private String database, hostname, username, password;
  27.     private int port;
  28.  
  29.     public MySQL(Plugin owner) {
  30.         executor = Executors.newCachedThreadPool();
  31.         plugin = owner;
  32.  
  33.         loadLoginInfo();
  34.         connect();
  35.     }
  36.  
  37.     private void loadLoginInfo() {
  38.         LoginInfoManager loginInfo = StatsAPI.getApi().loginInfo;
  39.         this.database = loginInfo.getDatabase();
  40.         this.hostname = loginInfo.getHostname();
  41.         this.username = loginInfo.getUsername();
  42.         this.password = loginInfo.getPassword();
  43.         this.port = loginInfo.getPort();
  44.     }
  45.  
  46.     private void connect() {
  47.         try {
  48.             Class.forName("com.mysql.jdbc.Driver");
  49.             connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.username, this.password);
  50.         } catch (SQLException | ClassNotFoundException e) {
  51.             e.printStackTrace();
  52.         }
  53.     }
  54.  
  55.     public void disconnect() {
  56.         try {
  57.             if (isConnected())
  58.                 connection.close();
  59.             connection = null;
  60.         } catch (SQLException e) {
  61.             e.printStackTrace();
  62.         }
  63.         this.executor.shutdown();
  64.     }
  65.  
  66.     public boolean isConnected() {
  67.         try {
  68.             boolean b = this.connection != null && !this.connection.isClosed();
  69.             return b;
  70.         } catch (SQLException e) {
  71.             return false;
  72.         }
  73.     }
  74.  
  75.     public void update(String query) {
  76.         // System.out.println(query);
  77.         if (isConnected())
  78.             executor.execute(() -> {
  79.                 try {
  80.                     Statement statement = connection.createStatement();
  81.                     statement.executeUpdate(query);
  82.                     statement.close();
  83.                 } catch (SQLException e) {
  84.                     e.printStackTrace();
  85.                 }
  86.             });
  87.         else
  88.             Bukkit.getLogger().warning("Could not execute update: '" + query + "'");
  89.     }
  90.  
  91.     public void updateSynchronously(String query) {
  92.         // System.out.println(query);
  93.         if (isConnected())
  94.             try {
  95.                 Statement statement = connection.createStatement();
  96.                 statement.executeUpdate(query);
  97.                 statement.close();
  98.             } catch (SQLException e) {
  99.                 e.printStackTrace();
  100.             }
  101.         else
  102.             Bukkit.getLogger().warning("Could not execute update: '" + query + "'");
  103.     }
  104.  
  105.     public void query(String query, Consumer<ResultSet> consumer) {
  106.         if (isConnected())
  107.             executor.execute(() -> {
  108.                 try {
  109.                     Statement st = connection.createStatement();
  110.                     ResultSet rs = st.executeQuery(query);
  111.                     Bukkit.getScheduler().runTask(plugin, () -> consumer.accept(rs));
  112.                 } catch (SQLException ex) {
  113.                     ex.printStackTrace();
  114.                 }
  115.             });
  116.         else
  117.             Bukkit.getLogger().warning("Could not execute query: '" + query + "'");
  118.     }
  119.  
  120.     public ResultSet query(String query) throws SQLException {
  121.         if (isConnected()) {
  122.             Statement st = connection.createStatement();
  123.             return st.executeQuery(query);
  124.         } else {
  125.             Bukkit.getLogger().warning("Could not execute query: '" + query + "'");
  126.             return null;
  127.         }
  128.     }
  129.  
  130.     public HashMap<String, String> getTableColumns(String table) {
  131.         if (isConnected())
  132.             try {
  133.                 ResultSet resultSet = connection.getMetaData().getColumns(this.database, null, table, "%");
  134.                 HashMap<String, String> cols = new HashMap<>();
  135.                 while (resultSet.next()) {
  136.                     cols.put(resultSet.getString(4), resultSet.getString(6));
  137.                 }
  138.                 return cols;
  139.             } catch (SQLException e) {
  140.                 e.printStackTrace();
  141.                 return null;
  142.             }
  143.         else {
  144.             Bukkit.getLogger().warning("Could not get columns of table '" + table + "'");
  145.             return null;
  146.         }
  147.     }
  148.  
  149.     public boolean tableExist(String tableName) {
  150.         if (isConnected()) {
  151.             boolean tExists = false;
  152.             try (ResultSet rs = connection.getMetaData().getTables(null, null, tableName, null)) {
  153.                 while (rs.next()) {
  154.                     String tName = rs.getString("TABLE_NAME");
  155.                     if (tName != null && tName.equals(tableName)) {
  156.                         tExists = true;
  157.                         break;
  158.                     }
  159.                 }
  160.             } catch (SQLException e) {
  161.                 e.printStackTrace();
  162.             }
  163.             return tExists;
  164.         } else {
  165.             Bukkit.getLogger().warning("Could not check if table '" + tableName + "' exists. Assuming it does.");
  166.             return true;
  167.         }
  168.     }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement