Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. public class MySQL
  2. {
  3. private static Connection con;
  4.  
  5. public static Connection getConnection() {
  6. return MySQL.con;
  7. }
  8.  
  9. public static void setConnection(final String host, final String user, final String password, final String database, final String port) {
  10. if (host == null || user == null || password == null || database == null) {
  11. return;
  12. }
  13. disconnect(false);
  14. try {
  15. MySQL.con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
  16. }
  17. catch (Exception e) {
  18. }
  19. }
  20.  
  21. public static void connect() {
  22. connect(true);
  23. }
  24.  
  25. private static void connect(final boolean message) {
  26. final String host = "";
  27. final String user = "";
  28. final String password = "";
  29. final String database = "";
  30. final String port = "3306";
  31. setConnection(host, user, password, database, port);
  32. }
  33.  
  34. public static void disconnect() {
  35. disconnect(true);
  36. }
  37.  
  38. private static void disconnect(final boolean message) {
  39. try {
  40. if (isConnected()) {
  41. MySQL.con.close();
  42. }
  43. else if (message) {
  44. Bukkit.getLogger().info(ChatColor.RED + "Odlaczono z bazy danych powod: No existing connection");
  45. }
  46. }
  47. catch (Exception e) {
  48. if (message) {
  49. Bukkit.getLogger().info(ChatColor.RED + "Blad laczenia z baza danych: " + e.getMessage());
  50. }
  51. }
  52. MySQL.con = null;
  53. }
  54.  
  55. public static void reconnect() {
  56. disconnect();
  57. connect();
  58. }
  59.  
  60. public static boolean isConnected() {
  61. return getConnection() != null;
  62. }
  63.  
  64. public static void update(final String command) {
  65. if (command == null) {
  66. return;
  67. }
  68. connect(false);
  69. try {
  70. final Statement st = getConnection().createStatement();
  71. st.executeUpdate(command);
  72. st.close();
  73. }
  74. catch (Exception e) {
  75. Bukkit.getLogger().info(ChatColor.RED + "MySQL Update:");
  76. Bukkit.getLogger().info(ChatColor.RED + "Komenda: " + command);
  77. Bukkit.getLogger().info(ChatColor.RED + "Blad: " + e.getMessage());
  78. }
  79. }
  80.  
  81. public static ResultSet query(final String command) {
  82. if (command == null) {
  83. return null;
  84. }
  85. connect(false);
  86. ResultSet rs = null;
  87. try {
  88. final Statement st = getConnection().createStatement();
  89. rs = st.executeQuery(command);
  90. }
  91. catch (Exception e) {
  92. Bukkit.getLogger().info(ChatColor.RED + "MySQL Query:");
  93. Bukkit.getLogger().info(ChatColor.RED + "Komenda: " + command);
  94. Bukkit.getLogger().info(ChatColor.RED + "Blad: " + e.getMessage());
  95. }
  96. return rs;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement