Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. package me.D4rkP1ka.BKM;
  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.ExecutorService;
  9. import java.util.concurrent.Executors;
  10. import java.util.concurrent.TimeUnit;
  11. import java.util.function.Consumer;
  12. import java.util.logging.Logger;
  13.  
  14. import net.md_5.bungee.BungeeCord;
  15. import net.md_5.bungee.api.plugin.Plugin;
  16. public class AsyncMySQL {
  17.  
  18.  
  19.  
  20.  
  21. private ExecutorService executor;
  22. private Plugin plugin;
  23. private MySQL sql;
  24. public AsyncMySQL(Plugin plugin, String host, int port, String user, String password, String database) {
  25. try {
  26. sql = new MySQL(host, port, user, password, database);
  27. executor = Executors.newCachedThreadPool();
  28. this.plugin = plugin;
  29. Logger.getLogger("").info("MySQL > Connected.");
  30. } catch (Exception e) {
  31. Logger.getLogger("").info("Error. Couldnt connect to your MySQL-DB.");
  32. Logger.getLogger("").info("Der Server stoppt nun.");
  33. BungeeCord.getInstance().stop();
  34. return;
  35. }
  36. }
  37.  
  38. public void update(PreparedStatement statement) {
  39. executor.execute(() -> sql.queryUpdate(statement));
  40. }
  41. public void update(String statement) {
  42. executor.execute(() -> sql.queryUpdate(statement));
  43. }
  44.  
  45. public void query(PreparedStatement statement, Consumer<ResultSet> consumer) {
  46. executor.execute(() -> {
  47. ResultSet result = sql.query(statement);
  48. BungeeCord.getInstance().getScheduler().runAsync(plugin, () -> consumer.accept(result));
  49. });
  50. }
  51.  
  52. public void query(String statement, Consumer<ResultSet> consumer) {
  53. executor.execute(() -> {
  54. ResultSet result = sql.query(statement);
  55. BungeeCord.getInstance().getScheduler().runAsync(plugin, () -> consumer.accept(result));
  56. });
  57. }
  58.  
  59. public PreparedStatement prepare(String query) {
  60. try {
  61. return sql.getConnection().prepareStatement(query);
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. return null;
  66. }
  67.  
  68. public MySQL getMySQL() {
  69. return sql;
  70. }
  71.  
  72.  
  73.  
  74. public static class MySQL {
  75.  
  76. private String host, user, password, database;
  77. private int port;
  78.  
  79. private Connection conn;
  80.  
  81. public MySQL(String host, int port, String user, String password, String database) throws Exception {
  82. this.host = host;
  83. this.port = port;
  84. this.user = user;
  85. this.password = password;
  86. this.database = database;
  87.  
  88. this.openConnection();
  89. }
  90.  
  91. public void queryUpdate(String query) {
  92. checkConnection();
  93. try (PreparedStatement statement = conn.prepareStatement(query)) {
  94. queryUpdate(statement);
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. }
  99.  
  100.  
  101.  
  102. public void queryUpdate(PreparedStatement statement) {
  103. checkConnection();
  104. try {
  105. statement.executeUpdate();
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. } finally {
  109. try {
  110. statement.close();
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. }
  115. }
  116.  
  117. public ResultSet query(String query) {
  118. checkConnection();
  119. try {
  120. return query(conn.prepareStatement(query));
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. return null;
  125. }
  126.  
  127. public ResultSet query(PreparedStatement statement) {
  128. checkConnection();
  129. try {
  130. return statement.executeQuery();
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. }
  134. return null;
  135. }
  136.  
  137. public Connection getConnection() {
  138. return this.conn;
  139. }
  140.  
  141. public void checkConnection() {
  142. try {
  143. if (this.conn == null || !this.conn.isValid(10) || this.conn.isClosed()) openConnection();
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. }
  147. }
  148.  
  149.  
  150.  
  151. public Connection openConnection() throws Exception {
  152. Class.forName("com.mysql.jdbc.Driver");
  153. return this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database+"?autoReconnect=true", this.user, this.password);
  154. }
  155.  
  156. public void closeConnection() {
  157. try {
  158. this.conn.close();
  159. } catch (SQLException e) {
  160. e.printStackTrace();
  161. } finally {
  162. this.conn = null;
  163. }
  164. }
  165. }
  166.  
  167.  
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement