Advertisement
Guest User

Untitled

a guest
Apr 15th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. package de.lorenzo.methods;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.UUID;
  9.  
  10. import org.bukkit.entity.Player;
  11.  
  12. public class MySQL {
  13.  
  14. public static String host = "localhost";
  15. public static String port = "3306";
  16. public static String database = "Nick";
  17. public static String username = "root";
  18. public static String password = "etygyfeL";
  19. public static Connection connection;
  20.  
  21. public static Connection getConnection() {
  22. return connection;
  23. }
  24.  
  25. public static void connectMySQL() {
  26. if (connection == null){
  27. try {
  28. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  29. } catch (SQLException ex) {
  30. ex.printStackTrace();
  31. }
  32. }
  33. }
  34.  
  35. public static void disconnect() {
  36. if (connection != null) {
  37. try {
  38. connection.close();
  39. } catch (SQLException ex) {
  40. ex.printStackTrace();
  41. }
  42. }
  43. }
  44.  
  45. public static void createTableIfNotExist() {
  46. if (connection != null) {
  47. try {
  48. PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Nick (Spielername VARCHAR(100), UUID VARCHAR(100))");
  49. ps.executeUpdate();
  50. ps.close();
  51. } catch (SQLException ex) {
  52. ex.printStackTrace();
  53. }
  54. }
  55. }
  56.  
  57. public static boolean isPlayerExisting(Player p) {
  58. try {
  59. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nick WHERE UUID = ?");
  60. ps.setString(1, p.toString());
  61. ResultSet result = ps.executeQuery();
  62. boolean isExisting = result.next();
  63. result.close();
  64. ps.close();
  65. return isExisting;
  66. } catch (Exception ex) {
  67. ex.printStackTrace();
  68. }
  69. return false;
  70. }
  71.  
  72. public static void registerPlayer(Player p) {
  73. if (isPlayerExisting(p)) {
  74. return;
  75. }
  76. try {
  77. PreparedStatement ps = connection.prepareStatement("INSERT INTO Nick (Spielername, UUID) VALUES (?, ?)");
  78. ps.setString(1, p.getName());
  79. ps.setString(2, p.getUniqueId().toString());
  80. ps.execute();
  81. ps.close();
  82. } catch (Exception ex) {
  83. ex.printStackTrace();
  84. }
  85. }
  86.  
  87. public static void setPlayerName(UUID uuid, String name) {
  88. try {
  89. PreparedStatement ps = connection.prepareStatement("UPDATE Nick SET Spielername = ? WHERE UUID = ?");
  90. ps.setString(1, name);
  91. ps.setString(2, uuid.toString());
  92. ps.executeUpdate();
  93. ps.close();
  94. } catch (Exception ex) {
  95. ex.printStackTrace();
  96. }
  97. }
  98.  
  99. public static void unregisterPlayer(Player p) {
  100. try {
  101. PreparedStatement ps = connection.prepareStatement("DELETE FROM Nick WHERE UUID = ?");
  102. ps.setString(1, p.getUniqueId().toString());
  103. ps.executeUpdate();
  104. ps.close();
  105. } catch (Exception ex) {
  106. ex.printStackTrace();
  107. }
  108. }
  109.  
  110.  
  111. //ONENABLE
  112.  
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement