Advertisement
Guest User

Untitled

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