Advertisement
Guest User

Untitled

a guest
Nov 30th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. package de.cadedev.skywars.sql;
  2.  
  3. import java.sql.*;
  4. import java.text.MessageFormat;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.locks.Condition;
  8. import java.util.concurrent.locks.Lock;
  9.  
  10. public class SQL {
  11.  
  12. private final String host;
  13. private final String user;
  14. private final String password;
  15. private final String database;
  16. private Connection connection;
  17. private final ExecutorService service = Executors.newCachedThreadPool();
  18.  
  19. public SQL(final String host, final String database, final String user, final String password) {
  20. this.host = host;
  21. this.database = database;
  22. this.user = user;
  23. this.password = password;
  24. }
  25.  
  26. public SQL startConnect() throws SQLException {
  27. final String url = MessageFormat.format("jdbc:mysql://{0}:{1}/{2}", this.host, String.valueOf(3306), this.database + "?autoReconnect=true");
  28. this.connection = DriverManager.getConnection(url, this.user, this.password);
  29. return this;
  30. }
  31.  
  32. public SQL startConnect(final Runnable runnable) throws SQLException {
  33. final String url = MessageFormat.format("jdbc:mysql://{0}:{1}/{2}", this.host, String.valueOf(3306), this.database + "?autoReconnect=true");
  34. this.connection = DriverManager.getConnection(url, this.user, this.password);
  35. runnable.run();
  36. return this;
  37. }
  38.  
  39. private boolean isConnected() {
  40.  
  41. try {
  42. return this.connection != null || !this.connection.isClosed();
  43. } catch (SQLException exe) {
  44. exe.printStackTrace();
  45. }
  46.  
  47. return false;
  48.  
  49. }
  50.  
  51. public void closeConnection() throws SQLException {
  52.  
  53. if (!isConnected()) {
  54. return;
  55. }
  56.  
  57. this.connection.close();
  58. this.service.shutdown();
  59. this.connection = null;
  60.  
  61. }
  62.  
  63. public void update(final PreparedStatement preparedStatement) {
  64. this.service.execute(() -> {
  65.  
  66. try {
  67. this.syncUpdate(preparedStatement);
  68. } catch (SQLException exe) {
  69. exe.printStackTrace();
  70. }
  71.  
  72. });
  73. }
  74.  
  75. private void syncUpdate(final PreparedStatement statement) throws SQLException {
  76. this.checkConnection();
  77. statement.executeUpdate();
  78. statement.close();
  79. }
  80.  
  81. public void checkConnection() throws SQLException {
  82. if (!isConnected()) {
  83. this.startConnect();
  84. }
  85. }
  86.  
  87. public void startQuery(final PreparedStatement query, final Callback<ResultSet> callback, final Lock lock, final Condition condition) {
  88. this.service.execute(new Query(query, callback, condition, lock, this));
  89. }
  90.  
  91. public void startQuery(final PreparedStatement query, final Callback<ResultSet> callback) {
  92. this.service.execute(new Query(query, callback, null, null, this));
  93. }
  94.  
  95. public PreparedStatement preparedStatement(final String update) {
  96.  
  97. try {
  98. return connection.prepareStatement(update);
  99. } catch (SQLException exe) {
  100. exe.printStackTrace();
  101. }
  102.  
  103. return null;
  104.  
  105. }
  106.  
  107. public class Query implements Runnable {
  108.  
  109. private final PreparedStatement query;
  110. private final Callback<ResultSet> callback;
  111. private final Condition condition;
  112. private final Lock lock;
  113. private SQL mysql;
  114.  
  115. public Query(final PreparedStatement query, final Callback<ResultSet> callback, final Condition condition, final Lock lock, final SQL mysql) {
  116. this.query = query;
  117. this.callback = callback;
  118. this.condition = condition;
  119. this.lock = lock;
  120. this.mysql = mysql;
  121. }
  122.  
  123. public void run() {
  124.  
  125. try {
  126. this.mysql.checkConnection();
  127. } catch (SQLException exe) {
  128. this.callback.onFaillure(exe.getCause());
  129. }
  130.  
  131. final PreparedStatement preparedStatement = this.query;
  132.  
  133. try {
  134.  
  135. final ResultSet resultset = preparedStatement.executeQuery();
  136.  
  137. if (resultset == null) {
  138.  
  139. this.callback.onFaillure(new NullPointerException());
  140.  
  141. } else {
  142.  
  143. this.callback.onSuccess(resultset);
  144.  
  145. if (this.condition != null || this.lock != null) {
  146.  
  147. this.lock.lock();
  148. this.condition.signal();
  149. this.lock.unlock();
  150.  
  151. }
  152.  
  153. }
  154.  
  155. } catch (SQLException exe) {
  156.  
  157. this.callback.onFaillure(exe.getCause());
  158.  
  159. } finally {
  160.  
  161. if (preparedStatement != null) {
  162.  
  163. try {
  164. preparedStatement.close();
  165. } catch (SQLException exe) {
  166. this.callback.onFaillure(exe.getCause());
  167. }
  168.  
  169. }
  170.  
  171. }
  172.  
  173. }
  174.  
  175. }
  176.  
  177. public interface Callback<T> {
  178. void onSuccess(final T resultSet);
  179. void onFaillure(final Throwable couse);
  180. }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement