Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. package com.mayhem.core.network.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import com.mayhem.core.util.Utility;
  9. import com.mayhem.rs2.content.membership.RankHandler;
  10. import com.mayhem.rs2.entity.World;
  11. import com.mayhem.rs2.entity.item.Item;
  12. import com.mayhem.rs2.entity.player.Player;
  13. import com.mayhem.rs2.entity.player.net.out.impl.SendMessage;
  14.  
  15. public class Voting implements Runnable {
  16.  
  17. public static final String HOST = "107.180.10.40";
  18. public static final String USER = "valorps_admin";
  19. public static final String PASS = "7GUZgAfcw6Ed3BQA";
  20. public static final String DATABASE = "valorps_vote4654";
  21.  
  22.  
  23.  
  24. private Player player;
  25. private Connection conn;
  26. private Statement stmt;
  27.  
  28. public Voting(Player player) {
  29. this.player = player;
  30. }
  31.  
  32. @Override
  33. public void run() {
  34. try {
  35. if (!connect(HOST, DATABASE, USER, PASS)) {
  36. return;
  37. }
  38.  
  39. String name = player.getUsername().replace(" ", "_");
  40. ResultSet rs = executeQuery("SELECT * FROM fx_votes WHERE username='"+name+"' AND claimed=0 AND callback_date IS NOT NULL");
  41.  
  42. while (rs.next()) {
  43. String timestamp = rs.getTimestamp("callback_date").toString();
  44. String ipAddress = rs.getString("ip_address");
  45. int siteId = rs.getInt("site_id");
  46.  
  47.  
  48. player.setVotePoints(player.getVotePoints() + 1);
  49.  
  50.  
  51.  
  52. System.out.println("Vote claimed by "+name+". (sid: "+siteId+", ip: "+ipAddress+", time: "+timestamp+")");
  53. player.send(new SendMessage("Thank you for voting!"));
  54. rs.updateInt("claimed", 1); // do not delete otherwise they can reclaim!
  55. rs.updateRow();
  56. }
  57.  
  58.  
  59. destroy();
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. }
  64.  
  65.  
  66. public boolean connect(String host, String database, String user, String pass) {
  67. try {
  68. this.conn = DriverManager.getConnection("jdbc:mysql://"+host+":3306/"+database, user, pass);
  69. return true;
  70. } catch (SQLException e) {
  71. System.out.println("Failing connecting to database!");
  72. return false;
  73. }
  74. }
  75.  
  76. public void destroy() {
  77. try {
  78. conn.close();
  79. conn = null;
  80. if (stmt != null) {
  81. stmt.close();
  82. stmt = null;
  83. }
  84. } catch(Exception e) {
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89. public int executeUpdate(String query) {
  90. try {
  91. this.stmt = this.conn.createStatement(1005, 1008);
  92. int results = stmt.executeUpdate(query);
  93. return results;
  94. } catch (SQLException ex) {
  95. ex.printStackTrace();
  96. }
  97. return -1;
  98. }
  99.  
  100. public ResultSet executeQuery(String query) {
  101. try {
  102. this.stmt = this.conn.createStatement(1005, 1008);
  103. ResultSet results = stmt.executeQuery(query);
  104. return results;
  105. } catch (SQLException ex) {
  106. ex.printStackTrace();
  107. }
  108. return null;
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement