Advertisement
Guest User

AsyncMYSQL

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