Advertisement
Guest User

sada

a guest
May 22nd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6.  
  7. import org.bukkit.entity.Player;
  8.  
  9. public class MysqlAPI {
  10.  
  11. private String url_base, host, name, username, password, table;
  12. private Connection connection;
  13.  
  14. public MysqlAPI(String url_base, String host, String name, String username, String password, String table) {
  15. this.url_base = url_base;
  16. this.host = host;
  17. this.name = name;
  18. this.username = username;
  19. this.password = password;
  20. this.table = table;
  21. }
  22.  
  23. public void connection() {
  24. if (!isConnected()) {
  25. try {
  26. connection = DriverManager.getConnection("jdbc:mysql://"+ "jdbc:mysql://" +":"+ "localhost" +"/"+ "nomedatabela" , "setaak o nome do player" , "");
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32.  
  33. public void deconnection() {
  34. if (isConnected()) {
  35. try {
  36. connection.close();
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42.  
  43. private boolean isConnected() {
  44. try {
  45. if ((connection == null) || (connection.isClosed()) || (!connection.isValid(5))) {
  46. return false;
  47. } else {
  48. return true;
  49. }
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. return false;
  54. }
  55.  
  56. private Connection getConnection() {
  57. return connection;
  58. }
  59.  
  60. public void createAccount(Player p, String rank) {
  61. try {
  62.  
  63. PreparedStatement sts = getConnection().prepareStatement("SELECT coins FROM " + table + " WHERE uuid = ?");
  64. sts.setString(1, p.getUniqueId().toString());
  65. ResultSet rs = sts.executeQuery();
  66. if (!rs.next()) {
  67. sts.close();
  68.  
  69. PreparedStatement sts2 = getConnection().prepareStatement("INSERT INTO "+table+"(uuid, kills, deaths, wins, coins, rank) VALUES (?, ?, ?, ?, ?, ?)");
  70. sts2.setString(1, p.getUniqueId().toString());
  71. sts2.setInt(2, 0);
  72. sts2.setInt(3, 0);
  73. sts2.setInt(4, 0);
  74. sts2.setInt(5, 0);
  75. sts2.setString(6, rank);
  76. sts2.executeUpdate();
  77. sts2.close();
  78. }
  79.  
  80. } catch (SQLException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85. public void addKills(String uuid, int kills) {
  86. try {
  87.  
  88. PreparedStatement sts = getConnection().prepareStatement("UPDATE "+table+" SET kills = kills + ? WHERE uuid = ?");
  89. sts.setInt(1, kills);
  90. sts.setString(2, uuid);
  91. sts.executeUpdate();
  92. sts.close();
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97.  
  98. public void addDeaths(String uuid, int deaths) {
  99. try {
  100. PreparedStatement sts = getConnection().prepareStatement("UPDATE "+table+" SET deaths = deaths + ? WHERE uuid = ?");
  101. sts.setInt(1, deaths);
  102. sts.setString(2, uuid);
  103. sts.executeUpdate();
  104. sts.close();
  105. } catch (SQLException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109.  
  110.  
  111. public void addCoins(String uuid, int coins) {
  112. try {
  113. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + table + " SET coins = coins + ? WHERE uuid = ?");
  114. sts.setInt(1, coins);
  115. sts.setString(2, uuid);
  116. sts.executeUpdate();
  117. sts.close();
  118. } catch (SQLException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122.  
  123. public void removeCoins(String uuid, int coins) {
  124. try {
  125. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + table + " SET coins = coins - ? WHERE uuid = ?");
  126. sts.setInt(1, coins);
  127. sts.setString(2, uuid);
  128. sts.executeUpdate();
  129. sts.close();
  130. } catch (SQLException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134.  
  135. public void setRank(String uuid, String rank) {
  136. try {
  137. PreparedStatement sts = getConnection().prepareStatement("UPDATE "+table+" SET rank = ? WHERE uuid = ?");
  138. sts.setString(1, rank);
  139. sts.setString(2, uuid);
  140. sts.executeUpdate();
  141. sts.close();
  142. } catch (SQLException e) {
  143. e.printStackTrace();
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement