Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. package de.shirro.lobbysystem.mysql;
  2.  
  3. import de.shirro.lobbysystem.LobbySystem;
  4. import org.bukkit.Bukkit;
  5.  
  6. import java.sql.*;
  7. import java.text.MessageFormat;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10. import java.util.concurrent.locks.Condition;
  11. import java.util.concurrent.locks.Lock;
  12.  
  13. public class MySQLManager {
  14.  
  15. private final String host;
  16. private final int port;
  17.  
  18. private final String database;
  19.  
  20. private final String username;
  21. private final String password;
  22.  
  23. private Connection con;
  24.  
  25. private final ExecutorService service = Executors.newCachedThreadPool();
  26.  
  27. public MySQLManager(final String host, final int port, final String database, final String username, final String password) {
  28.  
  29. this.host = host;
  30. this.port = port;
  31.  
  32. this.database = database;
  33.  
  34. this.username = username;
  35. this.password = password;
  36.  
  37. }
  38.  
  39. public MySQLManager openConnection() {
  40. final String url = MessageFormat.format("jdbc:mysql://{0}:{1}/{2}?autoReconnect=true&useUnicode=yes", this.host, String.valueOf(this.port), this.database);
  41. try {
  42. this.con = DriverManager.getConnection(url, this.username, this.password);
  43. } catch (SQLException e) {
  44. e.printStackTrace();
  45. }
  46. return this;
  47. }
  48.  
  49. public MySQLManager openConnection(final Runnable runable) throws SQLException {
  50. final String url = MessageFormat.format("jdbc:mysql://{0}:{1}/{2}?autoReconnect=true&useUnicode=yes", this.host, String.valueOf(this.port), this.database);
  51. this.con = DriverManager.getConnection(url, this.username, this.password);
  52. runable.run();
  53. return this;
  54. }
  55.  
  56. public void update(final PreparedStatement statement) {
  57. if(!LobbySystem.getInstance().isBackendUpdating()) {
  58. this.service.execute(() -> {
  59. try {
  60. this.syncUpdate(statement);
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. });
  65. } else {
  66. Bukkit.getScheduler().scheduleSyncDelayedTask(LobbySystem.getInstance(), () -> {
  67. this.service.execute(() -> {
  68. try {
  69. this.syncUpdate(statement);
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. });
  74. }, 20);
  75. }
  76. }
  77.  
  78. private void syncUpdate(final PreparedStatement statement) throws SQLException {
  79. this.checkConnection();
  80.  
  81. statement.executeUpdate();
  82. statement.close();
  83. }
  84.  
  85. public void query(final PreparedStatement query, final Callback<ResultSet> callback, final Lock lock, final Condition condition) {
  86. this.service.execute(new QueryRunnable(query, callback, this, lock, condition));
  87. }
  88.  
  89. public void query(final PreparedStatement query, final Callback<ResultSet> callback) {
  90. this.service.execute(new QueryRunnable(query, callback, this, null, null));
  91. }
  92.  
  93. public final PreparedStatement prepareStatement(final String update) {
  94. try {
  95. return this.con.prepareStatement(update);
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. }
  99. return null;
  100. }
  101.  
  102. public void checkConnection() throws SQLException {
  103. if(!this.isConnected())
  104. this.openConnection();
  105. }
  106.  
  107. public void closeConnection() {
  108. if(!this.isConnected())
  109. return;
  110.  
  111. try {
  112. this.con.close();
  113. } catch (SQLException e) {
  114. e.printStackTrace();
  115. }
  116. this.service.shutdown();
  117. this.con = null;
  118. }
  119.  
  120. private boolean isConnected() {
  121.  
  122. try {
  123. return this.con != null && !this.con.isClosed();
  124. } catch (SQLException e) {
  125. e.printStackTrace();
  126. }
  127.  
  128. return false;
  129. }
  130.  
  131. public final Connection getConnection() {
  132. return this.con;
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement