Advertisement
Guest User

Untitled

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