Advertisement
JulianGames

Bungee:MYSQL

Mar 29th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. package de.juliangames.bungee.sql;
  2.  
  3. import net.md_5.bungee.*;
  4. import net.md_5.bungee.api.chat.*;
  5. import java.sql.*;
  6. import java.io.*;
  7. import net.md_5.bungee.config.*;
  8.  
  9. public class MySQL
  10. {
  11. public static String username;
  12. public static String password;
  13. public static String database;
  14. public static String host;
  15. public static String port;
  16. public static Connection con;
  17.  
  18. public static void connect() {
  19. if (!isConnected()) {
  20. try {
  21. MySQL.con = DriverManager.getConnection("jdbc:mysql://" + MySQL.host + ":" + MySQL.port + "/" + MySQL.database + "?autoReconnect=true", MySQL.username, MySQL.password);
  22. BungeeCord.getInstance().getConsole().sendMessage((BaseComponent)new TextComponent("§8============================================================"));
  23. BungeeCord.getInstance().getConsole().sendMessage((BaseComponent)new TextComponent("§7Status: §aDie MySQL Verbindung wurde erfolgreich aufgebaut."));
  24. BungeeCord.getInstance().getConsole().sendMessage((BaseComponent)new TextComponent("§8============================================================"));
  25. }
  26. catch (SQLException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31.  
  32. public static void close() {
  33. if (isConnected()) {
  34. try {
  35. MySQL.con.close();
  36. BungeeCord.getInstance().getConsole().sendMessage((BaseComponent)new TextComponent("§8============================================================"));
  37. BungeeCord.getInstance().getConsole().sendMessage((BaseComponent)new TextComponent("§7Status: §cDie MySQL Verbindung wurde erfolgreich geschlossen."));
  38. BungeeCord.getInstance().getConsole().sendMessage((BaseComponent)new TextComponent("§8============================================================"));
  39. }
  40. catch (SQLException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  45.  
  46. public static boolean isConnected() {
  47. return MySQL.con != null;
  48. }
  49.  
  50. public static void update(final String query) {
  51. PreparedStatement ps = null;
  52. try {
  53. ps = MySQL.con.prepareStatement(query);
  54. ps.executeUpdate();
  55. }
  56. catch (SQLException e) {
  57. e.printStackTrace();
  58. try {
  59. ps.close();
  60. }
  61. catch (SQLException e1) {
  62. e.printStackTrace();
  63. }
  64. }
  65. finally {
  66. try {
  67. ps.close();
  68. }
  69. catch (SQLException e2) {
  70. e2.printStackTrace();
  71. }
  72. }
  73. }
  74.  
  75. public static ResultSet getResult(final String query) {
  76. PreparedStatement ps = null;
  77. ResultSet rs = null;
  78. try {
  79. ps = MySQL.con.prepareStatement(query);
  80. rs = ps.executeQuery();
  81. return rs;
  82. }
  83. catch (SQLException e) {
  84. e.printStackTrace();
  85. return null;
  86. }
  87. }
  88.  
  89. public static void createTables() {
  90. if (isConnected()) {
  91. try {
  92. MySQL.con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS BannedPlayers (Spielername VARCHAR(100), UUID VARCHAR(100), Ende VARCHAR(100), Grund VARCHAR(100), Banner VARCHAR(100))");
  93. MySQL.con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS MutedPlayers (Spielername VARCHAR(100), UUID VARCHAR(100), Ende VARCHAR(100), Grund VARCHAR(100), MUTER VARCHAR(100))");
  94. MySQL.con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS OnlineTime (Spielername VARCHAR(100), UUID VARCHAR(100), Zeit INT(100))");
  95. }
  96. catch (SQLException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. }
  101.  
  102. public static void loadFile() {
  103. try {
  104. final File file = new File("plugins//PC-Bungee", "MySQL.yml");
  105. boolean created = true;
  106. if (!file.exists()) {
  107. file.createNewFile();
  108. created = false;
  109. }
  110. final Configuration config = ConfigurationProvider.getProvider((Class)YamlConfiguration.class).load(file);
  111. if (!created) {
  112. config.set("mysql.Host", (Object)"91.218.67.112");
  113. config.set("mysql.Port", (Object)"3306");
  114. config.set("mysql.Database", (Object)"PC-Systeme");
  115. config.set("mysql.Username", (Object)"pandora");
  116. config.set("mysql.Password", (Object)"Quanitesedisitu");
  117. ConfigurationProvider.getProvider((Class)YamlConfiguration.class).save(config, file);
  118. }
  119. MySQL.host = config.getString("mysql.Host");
  120. MySQL.port = config.getString("mysql.Port");
  121. MySQL.database = config.getString("mysql.Database");
  122. MySQL.username = config.getString("mysql.Username");
  123. MySQL.password = config.getString("mysql.Password");
  124. }
  125. catch (Exception ex) {}
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement