Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. package de.godtitan.ot.util;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.concurrent.Callable;
  9. import java.util.concurrent.ExecutionException;
  10. import java.util.concurrent.ExecutorService;
  11. import java.util.concurrent.Executors;
  12. import java.util.concurrent.Future;
  13. import java.util.logging.Level;
  14.  
  15. import de.godtitan.ot.main.Main;
  16.  
  17. public class MySQL {
  18. private static MySQL instance;
  19.  
  20. private String host, database, user, password;
  21. private int port;
  22. private Connection conn;
  23.  
  24. public ExecutorService executor;
  25. private Main plugin;
  26.  
  27. public MySQL(Main plugin, String host, String database, String user, String password, int port) {
  28. this.host = host;
  29. this.database = database;
  30. this.user = user;
  31. this.password = password;
  32. this.port = port;
  33. this.plugin = plugin;
  34.  
  35. this.executor = Executors.newCachedThreadPool();
  36.  
  37. MySQL.instance = this;
  38. }
  39.  
  40. public void update(PreparedStatement stat) {
  41. if (this.isConnected()) {
  42. this.executor.execute(() -> this.queryUpdate(stat));
  43. }
  44. }
  45.  
  46. public void update(String stat) {
  47. if (this.isConnected()) {
  48. this.executor.execute(() -> this.queryUpdate(stat));
  49. }
  50. }
  51.  
  52. public ResultSet asnycquery(PreparedStatement stmt) {
  53. if (this.isConnected()) {
  54. Future<ResultSet> future = this.executor.submit(new Callable<ResultSet>() {
  55.  
  56. @Override
  57. public ResultSet call() throws Exception {
  58. return query(stmt);
  59. }
  60. });
  61. try {
  62. return future.get();
  63. } catch (InterruptedException | ExecutionException e) {
  64. e.printStackTrace();
  65. }
  66.  
  67. }
  68. return null;
  69. }
  70.  
  71. public ResultSet ASYNCquery(String statment) {
  72. if (this.isConnected()) {
  73. Future<ResultSet> future = this.executor.submit(new Callable<ResultSet>() {
  74.  
  75. @Override
  76. public ResultSet call() throws Exception {
  77. return query(statment);
  78. }
  79. });
  80. try {
  81. return future.get();
  82. } catch (InterruptedException | ExecutionException e) {
  83. e.printStackTrace();
  84. }
  85.  
  86. }
  87. return null;
  88. }
  89.  
  90. public PreparedStatement prepare(String query) {
  91. if (this.isConnected()) {
  92. try {
  93. return this.getConnection().prepareStatement(query);
  94. } catch (SQLException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. return null;
  99. }
  100.  
  101. public void queryUpdate(String query) {
  102. if (this.isConnected()) {
  103. try (PreparedStatement statment = this.conn.prepareStatement(query)) {
  104. this.queryUpdate(statment);
  105. } catch (Exception ex) {
  106. ex.printStackTrace();
  107. }
  108. }
  109. }
  110.  
  111. public ResultSet query(String query) {
  112. if (this.isConnected()) {
  113. try {
  114. return this.query(this.conn.prepareStatement(query));
  115. } catch (SQLException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. return null;
  120. }
  121.  
  122. public void queryUpdate(PreparedStatement stmt) {
  123. if (this.isConnected()) {
  124. try {
  125.  
  126. stmt.executeUpdate();
  127. } catch (SQLException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. }
  132.  
  133. public ResultSet query(PreparedStatement stat) {
  134. if (this.isConnected()) {
  135. try {
  136. return stat.executeQuery();
  137. } catch (SQLException e) {
  138. e.printStackTrace();
  139. }
  140. }
  141. return null;
  142. }
  143.  
  144. public boolean isConnected() {
  145. try {
  146. if (this.conn == null || !this.conn.isValid(10) || this.conn.isClosed()) {
  147. return false;
  148. }
  149. } catch (SQLException e) {
  150.  
  151. return false;
  152. }
  153. return true;
  154. }
  155.  
  156. public void connect() {
  157. try {
  158. this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database,
  159. this.user, this.password);
  160. this.plugin.getLogger().log(Level.INFO, "Verbindung mit MySQL aufgebaut!");
  161. } catch (SQLException e) {
  162. this.plugin.getLogger().log(Level.WARNING, "Fehler beim Verbinden mit MySQL");
  163.  
  164. }
  165. }
  166.  
  167. public Connection getConnection() {
  168. if (this.isConnected()) {
  169. return conn;
  170. }
  171. return null;
  172. }
  173.  
  174. public void closeConnection() {
  175. if (this.isConnected()) {
  176. try {
  177. this.conn.close();
  178. } catch (SQLException e) {
  179. } finally {
  180. this.conn = null;
  181. }
  182. }
  183. }
  184.  
  185. public static MySQL getInstance() {
  186. return instance;
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement