Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5.  
  6. import net.md_5.bungee.BungeeCord;
  7. import net.md_5.bungee.api.ChatColor;
  8.  
  9. public class MySQL
  10. {
  11. private static Connection con;
  12.  
  13. public static Connection getConnection() {
  14. return MySQL.con;
  15. }
  16.  
  17. public static void setConnection(final String host, final String user, final String password, final String database, final String port) {
  18. if (host == null || user == null || password == null || database == null) {
  19. return;
  20. }
  21. disconnect(false);
  22. try {
  23. MySQL.con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
  24. }
  25. catch (Exception e) {
  26. }
  27. }
  28.  
  29. public static void connect() {
  30. connect(true);
  31. }
  32.  
  33. private static void connect(final boolean message) {
  34. final String host = "";
  35. final String user = "";
  36. final String password = "";
  37. final String database = "";
  38. final String port = "3306";
  39. setConnection(host, user, password, database, port);
  40. }
  41.  
  42. public static void disconnect() {
  43. disconnect(true);
  44. }
  45.  
  46. @SuppressWarnings("deprecation")
  47. private static void disconnect(final boolean message) {
  48. try {
  49. if (isConnected()) {
  50. MySQL.con.close();
  51. BungeeCord.getInstance().getConsole().sendMessage(ChatColor.GREEN + "MySQL disconnected.");
  52. }
  53. else if (message) {
  54. BungeeCord.getInstance().getConsole().sendMessage(ChatColor.RED + "MySQL Disconnect Error: No existing connection");
  55. }
  56. }
  57. catch (Exception e) {
  58. if (message) {
  59. BungeeCord.getInstance().getConsole().sendMessage(ChatColor.RED + "MySQL Disconnect Error: " + e.getMessage());
  60. }
  61. }
  62. MySQL.con = null;
  63. }
  64.  
  65. public static void reconnect() {
  66. disconnect();
  67. connect();
  68. }
  69.  
  70. public static boolean isConnected() {
  71. return getConnection() != null;
  72. }
  73.  
  74. @SuppressWarnings("deprecation")
  75. public static void update(final String command) {
  76. if (command == null) {
  77. return;
  78. }
  79. connect(false);
  80. try {
  81. final Statement st = getConnection().createStatement();
  82. st.executeUpdate(command);
  83. st.close();
  84. }
  85. catch (Exception e) {
  86. BungeeCord.getInstance().getConsole().sendMessage(ChatColor.RED + "MySQL Update:");
  87. BungeeCord.getInstance().getConsole().sendMessage(ChatColor.RED + "Command: " + command);
  88. BungeeCord.getInstance().getConsole().sendMessage(ChatColor.RED + "Error: " + e.getMessage());
  89. }
  90. }
  91.  
  92. @SuppressWarnings("deprecation")
  93. public static ResultSet query(final String command) {
  94. if (command == null) {
  95. return null;
  96. }
  97. connect(false);
  98. ResultSet rs = null;
  99. try {
  100. final Statement st = getConnection().createStatement();
  101. rs = st.executeQuery(command);
  102. }
  103. catch (Exception e) {
  104. BungeeCord.getInstance().getConsole().sendMessage(ChatColor.RED + "MySQL Query:");
  105. BungeeCord.getInstance().getConsole().sendMessage(ChatColor.RED + "Command: " + command);
  106. BungeeCord.getInstance().getConsole().sendMessage(ChatColor.RED + "Error: " + e.getMessage());
  107. }
  108. return rs;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement