Advertisement
Guest User

Das ist die AsyncMySQL Klasse

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