Advertisement
Guest User

Untitled

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