Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. package de.DevEyes.MySQL;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.UUID;
  12.  
  13. import org.bukkit.Bukkit;
  14. import org.bukkit.configuration.file.FileConfiguration;
  15. import org.bukkit.configuration.file.YamlConfiguration;
  16.  
  17.  
  18. public class MySQL
  19. {
  20. public static String username;
  21. public static String password;
  22. public static String database;
  23. public static String host;
  24. public static String port;
  25. public static Connection con;
  26.  
  27. public MySQL(String user, String pass, String host2, String dB) {}
  28.  
  29. public static void connect()
  30. {
  31. if (!isConnected()) {
  32. try
  33. {
  34. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
  35. Bukkit.getConsoleSender().sendMessage("MySQL MySQL connected");
  36. }
  37. catch (SQLException e)
  38. {
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43.  
  44. public static void close()
  45. {
  46. if (isConnected()) {
  47. try
  48. {
  49. con.close();
  50. Bukkit.getConsoleSender().sendMessage("MySQL MySQL disconnected");
  51. }
  52. catch (SQLException e)
  53. {
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58.  
  59. public static boolean isConnected()
  60. {
  61. return con != null;
  62. }
  63.  
  64. public static void createTable()
  65. {
  66. if (isConnected()) {
  67. try
  68. {
  69. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS SkyPvP (UUID VARCHAR(100), NAME VARCHAR(80), KILLS int, DEATHS int , POINTS int)");
  70. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS BannedPlayers (Spielername VARCHAR(100), UUID VARCHAR(100), Ende VARCHAR(100), Grund VARCHAR(100))");
  71. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS MutedPlayers (Spielername VARCHAR(100), UUID VARCHAR(100), Ende VARCHAR(100), Grund VARCHAR(100))");
  72. Bukkit.getConsoleSender().sendMessage("MySQL MySQL Table created");
  73. }
  74. catch (SQLException e)
  75. {
  76. e.printStackTrace();
  77. }
  78. }
  79. }
  80.  
  81. public static void update(String qry)
  82. {
  83. if (isConnected()) {
  84. try
  85. {
  86. con.createStatement().executeUpdate(qry);
  87. }
  88. catch (SQLException e)
  89. {
  90. e.printStackTrace();
  91. }
  92. }
  93. }
  94.  
  95. public ResultSet query(String qry)
  96. {
  97. ResultSet rs = null;
  98. try
  99. {
  100. Statement st = con.createStatement();
  101. rs = st.executeQuery(qry);
  102. }
  103. catch (SQLException e)
  104. {
  105. connect();
  106. System.err.println(e);
  107. }
  108. return rs;
  109. }
  110.  
  111. public static ResultSet getResult(String qry)
  112. {
  113. ResultSet rs = null;
  114. try
  115. {
  116. Statement st = con.createStatement();
  117. rs = st.executeQuery(qry);
  118. }
  119. catch (SQLException e)
  120. {
  121. connect();
  122. System.err.println(e);
  123. }
  124. return rs;
  125. }
  126.  
  127. public static File getMySQLFile()
  128. {
  129. return new File("plugins/RiveMC", "MySQL.yml");
  130. }
  131.  
  132. public static FileConfiguration getMySQLFileConfiguration()
  133. {
  134. return YamlConfiguration.loadConfiguration(getMySQLFile());
  135. }
  136.  
  137. public static void setStandardMySQL()
  138. {
  139. FileConfiguration cfg = getMySQLFileConfiguration();
  140.  
  141. cfg.options().copyDefaults(true);
  142. cfg.addDefault("username", "root");
  143. cfg.addDefault("password", "password");
  144. cfg.addDefault("database", "localhost");
  145. cfg.addDefault("host", "localhost");
  146. cfg.addDefault("port", "3306");
  147. try
  148. {
  149. cfg.save(getMySQLFile());
  150. }
  151. catch (IOException e)
  152. {
  153. e.printStackTrace();
  154. }
  155. }
  156.  
  157. public static void readMySQL()
  158. {
  159. FileConfiguration cfg = getMySQLFileConfiguration();
  160. username = cfg.getString("username");
  161. password = cfg.getString("password");
  162. database = cfg.getString("database");
  163. host = cfg.getString("host");
  164. port = cfg.getString("port");
  165. }
  166.  
  167. public static String getPlayerGroup(UUID uuid)
  168. {
  169. try
  170. {
  171. PreparedStatement ps = con.prepareStatement("SELECT * FROM permissions_inheritance WHERE child = ?");
  172. ps.setString(1, uuid.toString());
  173. ResultSet result = ps.executeQuery();
  174. result.next();
  175. String rank = result.getString("parent");
  176. result.close();
  177. ps.close();
  178. return rank;
  179. }
  180. catch (SQLException localSQLException) {}
  181. return "Spieler";
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement