Advertisement
Guest User

Untitled

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