Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. package pl.kopara.api.bungee.mysql;
  2.  
  3. /**
  4. * Created by Wojtek on 09.02.2017.
  5. */
  6.  
  7. import net.md_5.bungee.BungeeCord;
  8. import pl.kopara.api.bungee.BungeePlugin;
  9. import pl.kopara.api.bungee.utils.Logger;
  10.  
  11. import java.sql.*;
  12.  
  13. import java.util.concurrent.Executor;
  14. import java.util.concurrent.Executors;
  15. import java.util.concurrent.TimeUnit;
  16.  
  17.  
  18. public class BungeeMySQL {
  19.  
  20. private final String host;
  21. private final String user;
  22. private final String pass;
  23. private final String name;
  24. private final int port;
  25. private final String prefix;
  26. private Connection conn;
  27. private Executor executor;
  28.  
  29.  
  30. public BungeeMySQL(final String host, final int port, final String user, final String pass, final String name, final String prefix) {
  31. super();
  32. this.executor = Executors.newFixedThreadPool(6);
  33. this.host = host;
  34. this.port = port;
  35. this.user = user;
  36. this.pass = pass;
  37. this.name = name;
  38. this.prefix = prefix;
  39. BungeeCord.getInstance().getScheduler().schedule(BungeePlugin.getPlugins(), new Runnable() {
  40. @Override
  41. public void run() {
  42. BungeePlugin.getMySQL().execute("SELECT CURTIME()");
  43. }
  44. }, 15, TimeUnit.MINUTES);
  45. }
  46.  
  47. public boolean connect(){
  48. final long time = System.currentTimeMillis();
  49. try {
  50. Class.forName("com.mysql.jdbc.Driver");
  51. Logger.debug("Try connect with MySQL");
  52. this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.name, this.user, this.pass);
  53. Logger.debug("Connected to the MySQL with ping: " + (System.currentTimeMillis() - time) + "ms!");
  54. return true;
  55. }
  56. catch (ClassNotFoundException e) {
  57. Logger.debug("JDBC driver not found Error: " + e.getMessage());
  58. e.printStackTrace();
  59. }
  60. catch (SQLException e2) {
  61. Logger.debug("Can not connect to a MySQL server! Error: " + e2.getMessage());
  62. e2.printStackTrace();
  63. BungeeCord.getInstance().stop();
  64. }
  65. return false;
  66. }
  67.  
  68. public void INSERT(String table, String values, String q) {
  69. executeUpdate("INSERT INTO `{P}" + table + "` (`id`," + values +") VALUES (NULL, '" + q + "');");
  70. }
  71.  
  72. public void DELETE(String table, String where, String name) {
  73. executeUpdate("DELETE FROM `{P}" +table + "` WHERE `" + where + "` ='" + name + "';");
  74. }
  75.  
  76. public void DELETE(String table, String where1, String name1, String where2, String name2) {
  77. executeUpdate("DELETE FROM `{P}" +table + "` WHERE `" + where1 + "` ='" + name1 + "' AND `" + where2 + "` ='" + name2 + "';");
  78. }
  79.  
  80. public void UPDATE(String table, String values, String where, String name) {
  81. executeUpdate("UPDATE `{P}" + table + "` SET " + values + " WHERE `" + where + "` ='" + name +"';");
  82. }
  83.  
  84. public void CREATETABLE(String table, String values) {
  85. executeUpdate("CREATE TABLE IF NOT EXISTS `{P}" + table + "` (`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, " + values + ");");
  86. }
  87.  
  88. public ResultSet SELECT(final String table, final String where, final String name) {
  89. return query("SELECT * FROM `{P}" + table + "` WHERE `" + where + "` ='" + name + "';");
  90. }
  91.  
  92. public ResultSet SELECT(final String table) {
  93. return query("SELECT * FROM `{P}" + table + "`;");
  94. }
  95.  
  96. public void executeUpdate(String q) {
  97. this.executor.execute(() -> update(q));
  98. }
  99.  
  100.  
  101. private void update(String query){
  102. try {
  103. if (!this.isConnected()) {
  104. Logger.debug("CONNECTION CLOSE OPEN!");
  105. connect();
  106. }
  107. final Statement st = this.conn.createStatement();
  108. st.executeUpdate(query.replace("{P}", this.prefix));
  109. st.close();
  110. } catch (SQLException e) {
  111. Logger.debug(e.getMessage());
  112. e.printStackTrace();
  113.  
  114. }
  115. }
  116.  
  117. private ResultSet query(String query){
  118. try {
  119. if (!this.isConnected()) {
  120. Logger.debug("CONNECTION CLOSE OPEN!");
  121. connect();
  122. }
  123. Statement st = this.conn.createStatement();
  124. return st.executeQuery(query.replace("{P}", this.prefix));
  125. } catch (SQLException e) {
  126. Logger.debug(e.getMessage());
  127. e.printStackTrace();
  128. return null;
  129. }
  130. }
  131.  
  132.  
  133. public Connection getConnecrion() {
  134. return this.conn;
  135. }
  136.  
  137. public boolean isConnected() {
  138. try {
  139. return !this.conn.isClosed() || this.conn == null;
  140. }
  141. catch (SQLException e) {
  142. e.printStackTrace();
  143. return false;
  144. }
  145. }
  146.  
  147. private void execute(String query){
  148. if(this.conn == null) connect();
  149. try {
  150. Statement st = this.conn.createStatement();
  151. st.execute(query);
  152. st.close();
  153. } catch (SQLException e) {
  154. e.printStackTrace();
  155. }
  156. }
  157.  
  158. public void disconnect() {
  159. if (this.conn != null) {
  160. try {
  161. this.conn.close();
  162. }
  163. catch (SQLException e) {
  164. Logger.debug("Can not close the connection to the MySQL Error: " + e.getMessage());
  165. e.printStackTrace();
  166. }
  167. }
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement