PlayerBank.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package me.km.playerbank;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.UUID;
  6. import me.km.databank.DataBank;
  7. public class PlayerBank implements IPlayerBank {
  8. private final PreparedStatement add;
  9. private final PreparedStatement changeName;
  10. private final PreparedStatement getId;
  11. private final PreparedStatement getUUIDfromID;
  12. private final PreparedStatement getNamefromID;
  13. private final PreparedStatement getUUID;
  14. public PlayerBank(DataBank databank) {
  15. databank.execute("CREATE TABLE IF NOT EXISTS players ("
  16. + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
  17. + "uuid_1 BIGINT NOT NULL, "
  18. + "uuid_2 BIGINT NOT NULL, "
  19. + "name VARCHAR(20) NOT NULL, "
  20. + "UNIQUE INDEX (uuid_1, uuid_2));");
  21. add = databank.prepareStatement("INSERT INTO players (uuid_1,uuid_2,name) VALUES (?,?,?);");
  22. changeName = databank.prepareStatement("UPDATE players SET name=? WHERE uuid_1=? AND uuid_2=?;");
  23. getId = databank.prepareStatement("SELECT id FROM players WHERE uuid_1=? AND uuid_2=?;");
  24. getUUIDfromID = databank.prepareStatement("SELECT uuid_1,uuid_2 FROM players WHERE id=?;");
  25. getNamefromID = databank.prepareStatement("SELECT name FROM players WHERE id=?;");
  26. getUUID = databank.prepareStatement("SELECT uuid_1,uuid_2 FROM players WHERE name=?;");
  27. }
  28. @Override
  29. public boolean add(UUID uuid, String name) {
  30. try {
  31. add.setLong(1, uuid.getLeastSignificantBits());
  32. add.setLong(2, uuid.getMostSignificantBits());
  33. add.setString(3, name);
  34. add.executeUpdate();
  35. return true;
  36. } catch(SQLException ex) {
  37. ex.printStackTrace();
  38. return false;
  39. }
  40. }
  41. @Override
  42. public void changeName(UUID uuid, String name) {
  43. try {
  44. changeName.setString(1, name);
  45. changeName.setLong(2, uuid.getLeastSignificantBits());
  46. changeName.setLong(3, uuid.getMostSignificantBits());
  47. changeName.executeUpdate();
  48. } catch(SQLException ex) {
  49. ex.printStackTrace();
  50. }
  51. }
  52. @Override
  53. public int getId(UUID uuid) {
  54. try {
  55. getId.setLong(1, uuid.getLeastSignificantBits());
  56. getId.setLong(2, uuid.getMostSignificantBits());
  57. try(ResultSet rs = getId.executeQuery()) {
  58. if(rs.next()) {
  59. return rs.getInt(1);
  60. }
  61. return -1;
  62. } catch(SQLException ex) {
  63. ex.printStackTrace();
  64. }
  65. } catch(SQLException ex) {
  66. ex.printStackTrace();
  67. }
  68. return -1;
  69. }
  70. @Override
  71. public UUID getUUIDfromID(int id) {
  72. try {
  73. getUUIDfromID.setInt(1, id);
  74. try(ResultSet rs = getUUIDfromID.executeQuery()) {
  75. if(rs.next()) {
  76. return new UUID(rs.getLong(2), rs.getLong(1));
  77. }
  78. } catch(SQLException ex) {
  79. ex.printStackTrace();
  80. }
  81. } catch(SQLException ex) {
  82. ex.printStackTrace();
  83. }
  84. return null;
  85. }
  86. @Override
  87. public String getNamefromID(int id) {
  88. try {
  89. getNamefromID.setInt(1, id);
  90. try(ResultSet rs = getNamefromID.executeQuery()) {
  91. if(rs.next()) {
  92. return rs.getString(1);
  93. }
  94. } catch(SQLException ex) {
  95. ex.printStackTrace();
  96. }
  97. } catch(SQLException ex) {
  98. ex.printStackTrace();
  99. }
  100. return "";
  101. }
  102. @Override
  103. public UUID getUUID(String name) {
  104. try {
  105. getUUID.setString(1, name);
  106. try(ResultSet rs = getUUID.executeQuery()) {
  107. if(rs.next()) {
  108. return new UUID(rs.getLong(2), rs.getLong(1));
  109. }
  110. } catch(Exception ex) {
  111. ex.printStackTrace();
  112. }
  113. } catch(SQLException ex) {
  114. ex.printStackTrace();
  115. }
  116. return null;
  117. }
  118. }