Guest User

Untitled

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