Guest User

Untitled

a guest
Feb 5th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. public class MySQL {
  2.  
  3. private static MySQL instance;
  4.  
  5. private String host, database, user, password;
  6. private int port;
  7. private static Connection con;
  8.  
  9. private ExecutorService executor;
  10. private Plugin plugin;
  11.  
  12. public MySQL(Plugin plugin, String host, String database, String user, String password, int port) {
  13. this.plugin = plugin;
  14. this.executor = Executors.newCachedThreadPool();
  15. this.host = host;
  16. this.database = database;
  17. this.user = user;
  18. this.password = password;
  19. this.port = port;
  20.  
  21. MySQL.instance = this;
  22. }
  23.  
  24. public void update(PreparedStatement stmt) {
  25. if (isConnected()) {
  26. executor.execute(() -> queryUpdate(stmt));
  27. }
  28. }
  29.  
  30. public void update(String stmt) {
  31. if (isConnected()) {
  32. executor.execute(() -> queryUpdate(stmt));
  33. }
  34. }
  35.  
  36. public void query(PreparedStatement stmt, Consumer<ResultSet> consumer) {
  37. if (isConnected()) {
  38. this.executor.execute(() -> {
  39. ResultSet rs = query(stmt);
  40. Bukkit.getScheduler().runTask(this.plugin, () -> {
  41. consumer.accept(rs);
  42. });
  43. });
  44. }
  45. }
  46.  
  47. public void query(String statement, Consumer<ResultSet> consumer) {
  48. if (isConnected()) {
  49. this.executor.execute(() -> {
  50. ResultSet rs = query(statement);
  51. Bukkit.getScheduler().runTask(this.plugin, () -> {
  52. consumer.accept(rs);
  53. });
  54. });
  55. }
  56. }
  57.  
  58. private void queryUpdate(String qry) {
  59. if (this.isConnected()) {
  60. try (PreparedStatement stmt = con.prepareStatement(qry)) {
  61. this.queryUpdate(stmt);
  62. } catch (SQLException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. }
  67.  
  68. public ResultSet query(String qry) {
  69. if (isConnected()) {
  70. try {
  71. return query(con.prepareStatement(qry));
  72. } catch (SQLException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. return null;
  77. }
  78.  
  79. private void queryUpdate(PreparedStatement stmt) {
  80. if (this.isConnected()) {
  81. try {
  82. stmt.executeUpdate();
  83. } catch (SQLException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. }
  88.  
  89. public ResultSet query(PreparedStatement stmt) {
  90. if (this.isConnected()) {
  91. try {
  92. return stmt.executeQuery();
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. return null;
  98. }
  99.  
  100. public static ResultSet getResult(String qry) {
  101. ResultSet rs = null;
  102. try {
  103. Statement st = con.createStatement();
  104. rs = st.executeQuery(qry);
  105. } catch (SQLException e) {
  106. e.printStackTrace();
  107. }
  108. return rs;
  109. }
  110.  
  111. private boolean isConnected() {
  112. try {
  113. if (this.con == null || !this.con.isValid(10) || this.con.isClosed()) {
  114. return false;
  115. }
  116. } catch (SQLException e) {
  117. e.printStackTrace();
  118. return false;
  119. }
  120. return true;
  121. }
  122.  
  123. public void closeConnection() {
  124. if (this.isConnected()) {
  125. try {
  126. this.con.close();
  127. } catch (SQLException e) {
  128. e.printStackTrace();
  129. } finally {
  130. this.con = null;
  131. }
  132. }
  133. }
  134.  
  135. public void connect() {
  136. try {
  137. con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password);
  138. Bukkit.getConsoleSender().sendMessage("§8[§aMySQL§8] §aEine Verbindung zur MySQL Datenbank wurde hergestellt");
  139. } catch (SQLException e) {
  140. Bukkit.getConsoleSender().sendMessage("§8[§aMySQL§8] §cKonnte keine Verbindung zur MySQL Datenbank herstellen, Fehler§8: §e" + e.getMessage());
  141. }
  142. }
  143.  
  144. public Connection getConnection() {
  145. if (this.isConnected()) {
  146. return con;
  147. }
  148. return null;
  149. }
  150.  
  151. public static PreparedStatement getStatement(String sql) {
  152. try {
  153. return con.prepareStatement(sql);
  154. } catch (SQLException e) {
  155. e.printStackTrace();
  156. }
  157. return null;
  158. }
  159.  
  160. public static MySQL getInstance() {
  161. return instance;
  162. }
  163. }
Add Comment
Please, Sign In to add comment