Advertisement
Guest User

Untitled

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