Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7.  
  8.  
  9.  
  10. public class Vote {
  11.  
  12.  
  13. private static Connection con = null;
  14. private static Statement stmt;
  15.  
  16. public static void createConnection() {
  17. try {
  18. Class.forName("com.mysql.jdbc.Driver").newInstance();
  19. String IP="WEB HOST IP";
  20. String DB="DATABASE NAME";
  21. String User="DATABASE USERNAME";
  22. String Pass="DATABASE PASSWORD";
  23. con = DriverManager.getConnection("jdbc:mysql://"+IP+"/"+DB, User, Pass);
  24. stmt = con.createStatement();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29.  
  30. public static void claimVote(Player player) {
  31. createConnection();
  32. if (checkVote(player.getUsername())) {
  33. int tokens = getVotes(player.getUsername()); //Number of times this user has voted. Number of unclaimed votes.
  34.  
  35. //methods for giving rewards to players goes here.
  36. //for example, I might give a user 50,000 coins per 1 time they voted, so I would do:
  37. player.getInventory().add(new Item(995, 50000 * tokens)); // adds 50,000 * token amount of coins to the player's inventory.
  38.  
  39.  
  40. //remove votes after user as claimed.
  41. removeVotes(player.getUsername());
  42. } else {
  43. player.sendMessage("You either have not voted, already claimed your reward, or there was an error.");
  44. player.sendMessage("Either try again in a few minutes, or contact a staff member.");
  45. }
  46. }
  47.  
  48. private static ResultSet query(String s) throws SQLException {
  49. try {
  50. if (s.toLowerCase().startsWith("select")) {
  51. ResultSet rs = stmt.executeQuery(s);
  52. return rs;
  53. } else {
  54. stmt.executeUpdate(s);
  55. }
  56. return null;
  57. } catch (Exception e) {
  58. destroyConnection();
  59. createConnection();
  60. }
  61. return null;
  62. }
  63.  
  64.  
  65. private static void destroyConnection() {
  66. try {
  67. stmt.close();
  68. con.close();
  69. } catch (Exception e) {
  70.  
  71.  
  72. }
  73. }
  74.  
  75.  
  76. private static boolean checkVote(String playerName) {
  77. try {
  78. String name2 = playerName.replaceAll("_", " ");
  79. Statement statement = con.createStatement();
  80. String query = "SELECT * FROM players WHERE username = '" + name2 + "'";
  81. ResultSet results = statement.executeQuery(query);
  82. while(results.next()) {
  83. int tokens = results.getInt("tokens");
  84. if(tokens >= 0) {
  85. return true;
  86. }
  87. }
  88. } catch(SQLException e) {
  89. e.printStackTrace();
  90. }
  91. return false;
  92. }
  93.  
  94. private static int getVotes(String playerName) {
  95. try {
  96. String name2 = playerName.replaceAll("_", " ");
  97. Statement statement = con.createStatement();
  98. String query = "SELECT * FROM players WHERE username = '" + name2 + "'";
  99. ResultSet results = statement.executeQuery(query);
  100. while(results.next()) {
  101. int tokens = results.getInt("tokens");
  102. if(tokens >= 1) {
  103. return tokens;
  104. }
  105. }
  106. } catch(SQLException e) {
  107. e.printStackTrace();
  108. }
  109. return 0;
  110. }
  111.  
  112. private static boolean removeVotes(String playerName) {
  113. try {
  114. String name2 = playerName.replaceAll("_", " ");
  115. query("DELETE FROM `players` WHERE username = '"+name2+"';");
  116. } catch (Exception e) {
  117. e.printStackTrace();
  118. return false;
  119. }
  120. return true;
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement