Advertisement
Guest User

Untitled

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