SergOmarov

Untitled

Sep 23rd, 2017
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package hohserg.economy;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4.  
  5. import java.sql.*;
  6.  
  7. public final class DatabaseConnector
  8. {
  9. private String table;
  10. private Connection connection;
  11.  
  12. public DatabaseConnector(String url, String user, String pass, String db, String table, short port) throws SQLException
  13. {
  14. try
  15. {
  16. Class.forName("com.mysql.jdbc.Driver");
  17. }
  18. catch (final ClassNotFoundException e)
  19. {
  20. e.printStackTrace();
  21. }
  22.  
  23. connection = DriverManager.getConnection("jdbc:mysql://" + url + ":" + port + "/" + db, user, pass);
  24.  
  25. this.table = table;
  26.  
  27. createTable(table);
  28. }
  29.  
  30. public void createTable(String table) throws SQLException
  31. {
  32. PreparedStatement statement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `" + table + "` (`name` CHAR(32) NOT NULL, `uuid` CHAR(36) NOT NULL, `laky` INT(11) NOT NULL DEFAULT 0, `arena` INT(11) NOT NULL DEFAULT 0, `donate` INT(11) NOT NULL DEFAULT 0, `game` INT(11) NOT NULL DEFAULT 0);");
  33. statement.executeUpdate();
  34. }
  35.  
  36. public int get(EntityPlayer player, MoneyType moneyType) throws SQLException
  37. {
  38. if (player != null)
  39. {
  40. PreparedStatement select = connection.prepareStatement(
  41. "SELECT " + moneyType.getName() + " FROM `" + table + "` WHERE `name` = '" + player.getDisplayName() + "' LIMIT 1"
  42. );
  43.  
  44. try (ResultSet set = select.executeQuery())
  45. {
  46. if (set.next())
  47. {
  48. return set.getInt(moneyType.getName());
  49. }
  50. else
  51. {
  52. return 0;
  53. }
  54. }
  55. }
  56. return 0;
  57. }
  58.  
  59. public void add(EntityPlayer player, MoneyType moneyType, int count) throws SQLException
  60. {
  61. if (player != null)
  62. {
  63. String
  64. name = player.getDisplayName(),
  65. uuid = player.getUniqueID().toString();
  66.  
  67. PreparedStatement select = connection.prepareStatement("SELECT * FROM `" + table + "` WHERE `name` = '" + name + "' LIMIT 1;");
  68.  
  69. try (ResultSet set = select.executeQuery())
  70. {
  71. count = validInt(count);
  72.  
  73. if (set.next())
  74. {
  75. count = set.getInt(moneyType.getName()) + count;
  76.  
  77. connection.prepareStatement(
  78. "UPDATE `" + table + "` SET `" + moneyType.getName() + "` = '" + count + "' WHERE `name` = '" + name + "';"
  79. ).executeUpdate();
  80. }
  81. else
  82. {
  83. connection.prepareStatement(
  84. "INSERT INTO `" + table + "` (`name`, `uuid`, `" + moneyType.getName() +"`) VALUES ('" + name + "', '" + uuid + "', '" + count + "');"
  85. ).executeUpdate();
  86. }
  87. Economy.update(player);
  88. }
  89. }
  90. }
  91.  
  92. public void remove(EntityPlayer player, MoneyType money, int count) throws SQLException
  93. {
  94. if (player != null)
  95. {
  96. String name = player.getDisplayName();
  97.  
  98. PreparedStatement select = connection.prepareStatement("SELECT * FROM `" + table + "` WHERE `name` = '" + name + "' LIMIT 1;");
  99.  
  100. try (ResultSet set = select.executeQuery())
  101. {
  102. count = validInt(count);
  103.  
  104. if (set.next())
  105. {
  106. count = set.getInt(money.getName()) - count;
  107.  
  108. connection.prepareStatement(
  109. "UPDATE `" + table + "` SET `" + money.getName() + "` = '" + count + "' WHERE `name` = '" + name + "';"
  110. ).executeUpdate();
  111. }
  112. Economy.update(player);
  113. }
  114. }
  115. }
  116.  
  117. public void delete(EntityPlayer player) throws SQLException
  118. {
  119. if (player != null)
  120. {
  121. connection.prepareStatement(
  122. "DELETE FROM `" + table + "` WHERE `uuid` = '" + player.getUniqueID().toString() + "';"
  123. ).executeUpdate();
  124. Economy.update(player);
  125. }
  126. }
  127.  
  128. private int validInt(int count)
  129. {
  130. return Math.abs(count);
  131. }
  132. }
RAW Paste Data