Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. if(!getDataFolder().exists()) {
  2. getDataFolder().mkdir();
  3. }
  4.  
  5. MySQL.loadFile();
  6. MySQL.connect();
  7. MySQL.createTables();
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14. public class MySQL
  15. {
  16. public static String username;
  17. public static String password;
  18. public static String database;
  19. public static String host;
  20. public static String port;
  21. public static Connection con;
  22.  
  23. public static void connect()
  24. {
  25. if (!isConnected()) {
  26. try
  27. {
  28. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
  29. BungeeCord.getInstance().getConsole().sendMessage("§aDie MySQL Verbindung wurde erfolgreich aufgebaut§8.");
  30. }
  31. catch (SQLException e)
  32. {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37.  
  38. public static void close()
  39. {
  40. if (isConnected()) {
  41. try
  42. {
  43. con.close();
  44. BungeeCord.getInstance().getConsole().sendMessage("§cDie MySQL Verbindung wurde erfolgreich geschlossen§8.");
  45. }
  46. catch (SQLException e)
  47. {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52.  
  53. public static boolean isConnected()
  54. {
  55. if (con != null) {
  56. return true;
  57. }
  58. return false;
  59. }
  60.  
  61. public static void update(String query)
  62. {
  63. PreparedStatement ps = null;
  64. try
  65. {
  66. ps = con.prepareStatement(query);
  67. ps.executeUpdate(); return;
  68. }
  69. catch (SQLException e)
  70. {
  71. e.printStackTrace();
  72. }
  73. finally
  74. {
  75. try
  76. {
  77. ps.close();
  78. }
  79. catch (SQLException e)
  80. {
  81. e.printStackTrace();
  82. }
  83. }
  84. }
  85.  
  86. public static ResultSet getResult(String query)
  87. {
  88. PreparedStatement ps = null;
  89. ResultSet rs = null;
  90. try
  91. {
  92. ps = con.prepareStatement(query);
  93. return ps.executeQuery();
  94. }
  95. catch (SQLException e)
  96. {
  97. e.printStackTrace();
  98. }
  99. return null;
  100. }
  101.  
  102. public static void createTables()
  103. {
  104. if (isConnected()) {
  105. try
  106. {
  107. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS cFriends_Users(Name VARCHAR(16), UUID VARCHAR(64), FList VARCHAR(20000), FRequests VARCHAR(20000), FRequest VARCHAR(10), FJump VARCHAR(10), FOnline VARCHAR(10), FSwitch VARCHAR(10), PInvite int, FConnect BIGINT, FcOnline VARCHAR(10), FServer VARCHAR(50));");
  108. }
  109. catch (SQLException e)
  110. {
  111. e.printStackTrace();
  112. }
  113. }
  114. }
  115.  
  116. public static void loadFile()
  117. {
  118. try
  119. {
  120. File file = new File(Friends.getInstance().getDataFolder(), "MySQL.yml");
  121. boolean created = true;
  122. if (!file.exists())
  123. {
  124. file.createNewFile();
  125. created = false;
  126. }
  127. Configuration config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
  128. if (!created)
  129. {
  130. config.set("mysql.Host", "localhost");
  131. config.set("mysql.Port", "3306");
  132. config.set("mysql.Database", "FreundeSystem");
  133. config.set("mysql.Username", "teamu");
  134. config.set("mysql.Password", "2ewv&Z3Ht6t5UX$");
  135.  
  136. ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, file);
  137. }
  138. host = config.getString("mysql.Host");
  139. port = config.getString("mysql.Port");
  140. database = config.getString("mysql.Database");
  141. username = config.getString("mysql.Username");
  142. password = config.getString("mysql.Password");
  143. }
  144. catch (Exception localException) {}
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement