Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. package com.aleksbertassi.sakurakiller.SQL;
  2.  
  3. import java.sql.*;
  4.  
  5. import org.bukkit.entity.*;
  6.  
  7. public class MySQL
  8. {
  9. private String user;
  10. private String password;
  11. private String database;
  12. private String host;
  13. private Connection connection;
  14. private Statement stmt;
  15.  
  16.  
  17. public MySQL(final String user, final String password, final String database, final String host) {
  18. try {
  19. this.user = user;
  20. this.password = password;
  21. this.database = database;
  22. this.host = host;
  23. this.connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, user, password);
  24. (this.stmt = this.connection.createStatement()).execute("CREATE TABLE IF NOT EXISTS killer (player VARCHAR(255), wins INTEGER)");
  25. }
  26. catch (SQLException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30.  
  31. public void addNew(final String player, final int wins) {
  32. try {
  33. Class.forName("com.mysql.jdbc.Driver");
  34. final String sql = "INSERT INTO killer (player, wins) VALUES ('" + player + "', '" + wins + "');";
  35. this.stmt.executeUpdate(sql);
  36. }
  37. catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. public void updateWins(final String player, final int wins) {
  43. try {
  44. Class.forName("com.mysql.jdbc.Driver");
  45. final String sql = "UPDATE killer SET wins='" + (this.getWins(player) + wins) + "' WHERE player='" + player + "';";
  46. this.stmt.executeUpdate(sql);
  47. }
  48. catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. public int getWins(final String player) {
  54. try {
  55. Class.forName("com.mysql.jdbc.Driver");
  56. final String sql = "SELECT wins FROM killer WHERE player='" + player + "';";
  57. final ResultSet rs = this.stmt.executeQuery(sql);
  58. if (rs.next()) {
  59. return rs.getInt("wins");
  60. }
  61. }
  62. catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. return 0;
  66. }
  67.  
  68. public boolean hasClan(final String player) {
  69. try {
  70. Class.forName("com.mysql.jdbc.Driver");
  71. final String sql = "SELECT * FROM killer WHERE player='" + player + "'";
  72. final ResultSet rs = this.stmt.executeQuery(sql);
  73. return rs.next() && rs.getString("player").equalsIgnoreCase(player);
  74. }
  75. catch (Exception e) {
  76. e.printStackTrace();
  77. return false;
  78. }
  79. }
  80.  
  81. public void getTOPWins(final Player p) {
  82. try {
  83. p.sendMessage("§5»§d TOP 10 VENCEDORES DO EVENTO KILLER:");
  84. Class.forName("com.mysql.jdbc.Driver");
  85. final String sql = "SELECT * FROM killer ORDER BY wins DESC LIMIT 10";
  86. final ResultSet rs = this.stmt.executeQuery("SELECT * FROM killer ORDER BY wins DESC LIMIT 10");
  87. final String sendM = "&5[@posicao] &d@player &d» &f@vitorias &dvitórias.";
  88. int i = 0;
  89. if (rs.next()) {
  90. while (rs.next()) {
  91. ++i;
  92. p.sendMessage(sendM.replace("&", "§").replace("@posicao", new StringBuilder(String.valueOf(i)).toString()).replace("@clan", rs.getString("player")).replace("@vitorias", new StringBuilder(String.valueOf(rs.getInt("wins"))).toString()));
  93. }
  94. }
  95. else {
  96. p.sendMessage(sendM.replace("&", "§").replace("@posicao", new StringBuilder(String.valueOf(i)).toString()).replace("@clan", rs.getString("player")).replace("@vitorias", new StringBuilder(String.valueOf(rs.getInt("wins"))).toString()));
  97. }
  98. }
  99. catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. }
  103.  
  104. public void addWinnerPoint(final String clan) {
  105. if (this.hasClan(clan)) {
  106. this.updateWins(clan, 1);
  107. }
  108. else {
  109. this.addNew(clan, 1);
  110. }
  111. }
  112.  
  113. public void purgeRows() {
  114. try {
  115. Class.forName("com.mysql.jdbc.Driver");
  116. final String sql = "DELETE FROM killer;";
  117. this.stmt.executeUpdate("DELETE FROM killer;");
  118. }
  119. catch (Exception e) {
  120. e.printStackTrace();
  121. }
  122. }
  123.  
  124. public String getUser() {
  125. return this.user;
  126. }
  127.  
  128. public void setUser(final String user) {
  129. this.user = user;
  130. }
  131.  
  132. public String getPassword() {
  133. return this.password;
  134. }
  135.  
  136. public void setPassword(final String password) {
  137. this.password = password;
  138. }
  139.  
  140. public String getDatabase() {
  141. return this.database;
  142. }
  143.  
  144. public void setDatabase(final String database) {
  145. this.database = database;
  146. }
  147.  
  148. public String getHost() {
  149. return this.host;
  150. }
  151.  
  152. public void setHost(final String host) {
  153. this.host = host;
  154. }
  155.  
  156. public Connection getConnection() {
  157. return this.connection;
  158. }
  159.  
  160. public void setConnection(final Connection connection) {
  161. this.connection = connection;
  162. }
  163.  
  164. public Statement getStmt() {
  165. return this.stmt;
  166. }
  167.  
  168. public void setStmt(final Statement stmt) {
  169. this.stmt = stmt;
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement