Advertisement
Guest User

Untitled

a guest
Mar 14th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. package de.jonxthxnlf.oneline.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.configuration.file.YamlConfiguration;
  14.  
  15. import de.jonxthxnlf.oneline.main.Main;
  16.  
  17. public class MySQL {
  18.  
  19. public static Connection con;
  20.  
  21. public static void registerMySQLConfig() {
  22. File file = new File("plugins//OneLine", "config.yml");
  23. YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  24.  
  25. cfg.addDefault("mysql.host", "localhost");
  26. cfg.addDefault("mysql.database", "oneline");
  27. cfg.addDefault("mysql.user", "root");
  28. cfg.addDefault("mysql.password", "AmyMustang21");
  29. cfg.addDefault("mysql.port", 3306);
  30.  
  31. cfg.options().copyDefaults(true);
  32.  
  33. try {
  34. cfg.save(file);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38.  
  39. }
  40.  
  41. public MySQL() {
  42. connect();
  43. }
  44.  
  45. public void connect() {
  46. try {
  47. con = DriverManager.getConnection(
  48. "jdbc:mysql://" + "localhost" + ":" + "3306" + "/" + "oneline" + "?autoReconnect=true", "root", "AmyMustang21");
  49. System.out.println("MySQL - Erfolgreich hergestellt");
  50. } catch (SQLException e) {
  51. System.out.println("MySQL - Fehler | Fehler: " + e.getMessage());
  52. }
  53. }
  54.  
  55. public void close() {
  56. try {
  57. if (con != null) {
  58. con.close();
  59. System.out.println("[MySQL] Die Verbindung zur MySQL wurde Erfolgreich beendet!");
  60. }
  61. } catch (SQLException e) {
  62. System.out.println("[MySQL] Fehler beim beenden der Verbindung zur MySQL! Fehler: " + e.getMessage());
  63. }
  64. }
  65.  
  66. public void update(String qry) {
  67. try {
  68. Statement st = con.createStatement();
  69. st.executeUpdate(qry);
  70. st.close();
  71. } catch (SQLException e) {
  72. connect();
  73. System.err.println(e);
  74. }
  75. }
  76.  
  77. public ResultSet query(String qry) {
  78. ResultSet rs = null;
  79. try {
  80. Statement st = con.createStatement();
  81. rs = st.executeQuery(qry);
  82. } catch (SQLException e) {
  83. connect();
  84. System.err.println(e);
  85. }
  86. return rs;
  87. }
  88.  
  89. public static int getRank(String uuid) {
  90. int rank = 0;
  91. try {
  92. PreparedStatement ps = (PreparedStatement) con
  93. .prepareStatement("SELECT * FROM oneline ORDER BY POINTS DESC");
  94. ResultSet result = ps.executeQuery();
  95. while (result.next()) {
  96. rank++;
  97. String uuid2 = result.getString("UUID");
  98. if (uuid2.equalsIgnoreCase(uuid)) {
  99. return rank;
  100.  
  101. }
  102. }
  103. result.close();
  104. ps.close();
  105. } catch (Exception ex) {
  106. ex.printStackTrace();
  107. }
  108. return rank;
  109. }
  110.  
  111. public static UUID getRank(int id) {
  112. int i = 0;
  113. ResultSet rs = Main.mysql.query("SELECT * FROM oneline ORDER BY POINTS DESC LIMIT " + id);
  114. try {
  115. while (rs.next()) {
  116. i++;
  117. if (i == id) {
  118. return UUID.fromString(rs.getString("UUID"));
  119. }
  120. }
  121. } catch (SQLException e) {
  122. e.printStackTrace();
  123. }
  124. return null;
  125.  
  126. }
  127.  
  128. public void reconnect() {
  129. if (con == null) {
  130. connect();
  131. }
  132.  
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement