Advertisement
Guest User

Untitled

a guest
Feb 11th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. package pl.xores.sejfy.mysql.store.modes;
  2.  
  3. import java.util.concurrent.atomic.*;
  4. import java.util.concurrent.*;
  5. import org.bukkit.scheduler.*;
  6. import pl.xores.sejfy.*;
  7. import org.bukkit.plugin.*;
  8. import pl.xores.sejfy.utils.*;
  9. import java.sql.*;
  10. import pl.xores.sejfy.mysql.store.*;
  11.  
  12. public class StoreMySQL implements Store
  13. {
  14. private final String host;
  15. private final String user;
  16. private final String pass;
  17. private final String name;
  18. private final String prefix;
  19. private final int port;
  20. private Connection conn;
  21. private long time;
  22. private Executor executor;
  23. private AtomicInteger ai;
  24.  
  25. public StoreMySQL(final String host, final int port, final String user, final String pass, final String name, final String prefix) {
  26. this.host = host;
  27. this.port = port;
  28. this.user = user;
  29. this.pass = pass;
  30. this.name = name;
  31. this.prefix = prefix;
  32. this.executor = Executors.newSingleThreadExecutor();
  33. this.time = System.currentTimeMillis();
  34. this.ai = new AtomicInteger();
  35. new BukkitRunnable() {
  36. public void run() {
  37. if (System.currentTimeMillis() - StoreMySQL.this.time > TimeUtil.SECOND.getTime(30)) {
  38. StoreMySQL.this.update(false, "DO 1");
  39. }
  40. }
  41. }.runTaskTimer((Plugin)Main.getInst(), 600L, 600L);
  42. }
  43.  
  44. @Override
  45. public boolean connect() {
  46. final Timming t = new Timming("MySQL ping").start();
  47. try {
  48. Class.forName("com.mysql.jdbc.Driver");
  49. Logger.info("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.name, this.user, this.pass);
  50. this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.name, this.user, this.pass);
  51. Logger.info("Connected to the MySQL server!", "Connection ping " + t.stop().getExecutingTime() + "ms!");
  52. return true;
  53. }
  54. catch (ClassNotFoundException e) {
  55. Logger.warning("JDBC driver not found!", "Error: " + e.getMessage());
  56. e.printStackTrace();
  57. }
  58. catch (SQLException e2) {
  59. Logger.warning("Can not connect to a MySQL server!", "Error: " + e2.getMessage());
  60. e2.printStackTrace();
  61. }
  62. return false;
  63. }
  64.  
  65. @Override
  66. public void update(final boolean now, final String update) {
  67. this.time = System.currentTimeMillis();
  68. final Runnable r = new Runnable() {
  69. @Override
  70. public void run() {
  71. try {
  72. StoreMySQL.this.conn.createStatement().executeUpdate(update.replace("{P}", StoreMySQL.this.prefix));
  73. }
  74. catch (SQLException e) {
  75. Logger.warning("An error occurred with given query '" + update.replace("{P}", StoreMySQL.this.prefix) + "'!", "Error: " + e.getMessage());
  76. e.printStackTrace();
  77. }
  78. }
  79. };
  80. if (now) {
  81. r.run();
  82. }
  83. else {
  84. this.executor.execute(r);
  85. }
  86. }
  87.  
  88. @Override
  89. public ResultSet update(final String update) {
  90. try {
  91. final Statement statement = this.conn.createStatement();
  92. statement.executeUpdate(update.replace("{P}", this.prefix), 1);
  93. final ResultSet rs = statement.getGeneratedKeys();
  94. if (rs.next()) {
  95. return rs;
  96. }
  97. }
  98. catch (SQLException e) {
  99. Logger.warning("An error occurred with given query '" + update.replace("{P}", this.prefix) + "'!", "Error: " + e.getMessage());
  100. e.printStackTrace();
  101. }
  102. return null;
  103. }
  104.  
  105. @Override
  106. public void disconnect() {
  107. if (this.conn != null) {
  108. try {
  109. this.conn.close();
  110. }
  111. catch (SQLException e) {
  112. Logger.warning("Can not close the connection to the MySQL server!", "Error: " + e.getMessage());
  113. e.printStackTrace();
  114. }
  115. }
  116. }
  117.  
  118. @Override
  119. public void reconnect() {
  120. this.connect();
  121. }
  122.  
  123. @Override
  124. public boolean isConnected() {
  125. try {
  126. return !this.conn.isClosed() || this.conn == null;
  127. }
  128. catch (SQLException e) {
  129. e.printStackTrace();
  130. return false;
  131. }
  132. }
  133.  
  134. @Override
  135. public ResultSet query(final String query) {
  136. try {
  137. return this.conn.createStatement().executeQuery(query.replace("{P}", this.prefix));
  138. }
  139. catch (SQLException e) {
  140. Logger.warning("An error occurred with given query '" + query.replace("{P}", this.prefix) + "'!", "Error: " + e.getMessage());
  141. e.printStackTrace();
  142. return null;
  143. }
  144. }
  145.  
  146. @Override
  147. public void query(final String query, final Callback<ResultSet> cb) {
  148. new Thread(new Runnable() {
  149. @Override
  150. public void run() {
  151. try {
  152. final ResultSet rs = StoreMySQL.this.conn.createStatement().executeQuery(query.replace("{P}", StoreMySQL.this.prefix));
  153. cb.done(rs);
  154. }
  155. catch (SQLException e) {
  156. Logger.warning("An error occurred with given query '" + query.replace("{P}", StoreMySQL.this.prefix) + "'!", "Error: " + e.getMessage());
  157. cb.error(e);
  158. e.printStackTrace();
  159. }
  160. }
  161. }, "MySQL Thread #" + this.ai.getAndIncrement()).start();
  162. }
  163.  
  164. @Override
  165. public Connection getConnection() {
  166. return this.conn;
  167. }
  168.  
  169. @Override
  170. public StoreMode getStoreMode() {
  171. return StoreMode.MYSQL;
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement