Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. package me.erkes.survivalgames.mysql;
  2.  
  3. import org.bukkit.configuration.file.*;
  4. import java.io.*;
  5. import java.util.concurrent.*;
  6. import me.erkes.survivalgames.*;
  7. import org.bukkit.*;
  8. import java.util.function.*;
  9. import java.sql.*;
  10. import org.bukkit.plugin.*;
  11.  
  12. public class MySQL
  13. {
  14. File file;
  15. FileConfiguration cfg;
  16. private String HOST;
  17. private String DATABASE;
  18. private String USER;
  19. private String PASSWORD;
  20. private Connection con;
  21. private ExecutorService executor;
  22.  
  23. public MySQL() {
  24. this.file = new File("plugins/SurvivalGames/", "MySQL.yml");
  25. this.cfg = (FileConfiguration)YamlConfiguration.loadConfiguration(this.file);
  26. this.HOST = "";
  27. this.DATABASE = "";
  28. this.USER = "";
  29. this.PASSWORD = "";
  30. if (!this.file.exists()) {
  31. this.cfg.set("mysql.host", (Object)"host");
  32. this.cfg.set("mysql.database", (Object)"database");
  33. this.cfg.set("mysql.user", (Object)"user");
  34. this.cfg.set("mysql.password", (Object)"password");
  35. try {
  36. this.cfg.save(this.file);
  37. }
  38. catch (IOException ex) {
  39. ex.printStackTrace();
  40. }
  41. }
  42. this.HOST = this.cfg.getString("mysql.host");
  43. this.DATABASE = this.cfg.getString("mysql.database");
  44. this.USER = this.cfg.getString("mysql.user");
  45. this.PASSWORD = this.cfg.getString("mysql.password");
  46. this.connect();
  47. }
  48.  
  49. public void connect() {
  50. try {
  51. this.con = DriverManager.getConnection("jdbc:mysql://" + this.HOST + ":3306/" + this.DATABASE + "?autoReconnect=true", this.USER, this.PASSWORD);
  52. this.executor = Executors.newCachedThreadPool();
  53. System.out.println("[SURVIVALGAMES-MySQL] VERBINDUNG HERGESTELLLT!");
  54. }
  55. catch (SQLException e) {
  56. Bukkit.broadcastMessage(SurvivalGames.prefix + "§cMySQL Daten stimmen nicht!");
  57. System.out.println("[SURVIVALGAMES-MySQL] VERBINDUNG FEHLGESCHLAGEN! FEHLER: " + e.getMessage());
  58. }
  59. }
  60.  
  61. public void close() {
  62. try {
  63. if (this.con != null) {
  64. this.con.close();
  65. System.out.println("[SURVIVALGAMES-MySQL] VERBINDUNG ERFOLGREICH UNTERBROCHEN!");
  66. }
  67. }
  68. catch (SQLException e) {
  69. System.out.println("[SURVIVALGAMES-MySQL] VERBINDUNGSTRENNUNG FEHLGESCHLAGEN! FEHLER: " + e.getMessage());
  70. }
  71. }
  72.  
  73. public void update(final PreparedStatement statement) {
  74. this.executor.execute(() -> SurvivalGames.mysql.queryUpdate(statement));
  75. }
  76.  
  77. public void update(final String statement) {
  78. this.executor.execute(() -> SurvivalGames.mysql.queryUpdate(statement));
  79. }
  80.  
  81. public void query(final PreparedStatement statement, final Consumer<ResultSet> consumer) {
  82. final ResultSet result;
  83. this.executor.execute(() -> {
  84. result = SurvivalGames.mysql.query(statement);
  85. Bukkit.getScheduler().runTask((Plugin)SurvivalGames.m, () -> consumer.accept(result));
  86. });
  87. }
  88.  
  89. public void query(final String statement, final Consumer<ResultSet> consumer) {
  90. final ResultSet result;
  91. this.executor.execute(() -> {
  92. result = SurvivalGames.mysql.query(statement);
  93. Bukkit.getScheduler().runTask((Plugin)SurvivalGames.m, () -> consumer.accept(result));
  94. });
  95. }
  96.  
  97. public PreparedStatement prepare(final String query) {
  98. try {
  99. return SurvivalGames.mysql.con.prepareStatement(query);
  100. }
  101. catch (Exception e) {
  102. e.printStackTrace();
  103. return null;
  104. }
  105. }
  106.  
  107. public void queryUpdate(final String query) {
  108. this.checkConnection();
  109. try (final PreparedStatement statement = this.con.prepareStatement(query)) {
  110. this.queryUpdate(statement);
  111. }
  112. catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. }
  116.  
  117. public void queryUpdate(final PreparedStatement statement) {
  118. this.checkConnection();
  119. try {
  120. statement.executeUpdate();
  121. }
  122. catch (Exception e) {
  123. e.printStackTrace();
  124. try {
  125. statement.close();
  126. }
  127. catch (Exception e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. finally {
  132. try {
  133. statement.close();
  134. }
  135. catch (Exception e2) {
  136. e2.printStackTrace();
  137. }
  138. }
  139. }
  140.  
  141. public ResultSet query(final String query) {
  142. this.checkConnection();
  143. try {
  144. return this.query(this.con.prepareStatement(query));
  145. }
  146. catch (Exception e) {
  147. e.printStackTrace();
  148. return null;
  149. }
  150. }
  151.  
  152. public ResultSet query(final PreparedStatement statement) {
  153. this.checkConnection();
  154. try {
  155. return statement.executeQuery();
  156. }
  157. catch (Exception e) {
  158. e.printStackTrace();
  159. return null;
  160. }
  161. }
  162.  
  163. private void checkConnection() {
  164. try {
  165. if (this.con == null || !this.con.isValid(10) || this.con.isClosed()) {
  166. this.openConnection();
  167. }
  168. }
  169. catch (Exception e) {
  170. e.printStackTrace();
  171. }
  172. }
  173.  
  174. public Connection openConnection() throws Exception {
  175. Class.forName("com.mysql.jdbc.Driver");
  176. return this.con = DriverManager.getConnection("jdbc:mysql://" + this.HOST + ":" + 3306 + "/" + this.DATABASE, this.USER, this.PASSWORD);
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement