Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. package com.foxtrot.cores.mysql.impl;
  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.  
  9. import com.foxtrot.game.World;
  10. import com.foxtrot.game.player.Player;
  11.  
  12. public class FoxVote implements Runnable {
  13.  
  14. public static final String HOST = "";
  15. public static final String USER = "";
  16. public static final String PASS = "";
  17. public static final String DATABASE = "";
  18.  
  19. private Player player;
  20. private Connection conn;
  21. private Statement stmt;
  22.  
  23. public FoxVote(Player player) {
  24. this.player = player;
  25. }
  26.  
  27. @Override
  28. public void run() {
  29. try {
  30. if (!connect(HOST, DATABASE, USER, PASS)) {
  31. return;
  32. }
  33.  
  34. String name = player.getUsername().replace(" ", "_");
  35. ResultSet rs = executeQuery("SELECT * FROM fx_votes WHERE username='"+name+"' AND claimed=0 AND callback_date IS NOT NULL");
  36.  
  37. while (rs.next()) {
  38. String timestamp = rs.getTimestamp("callback_date").toString();
  39. String ipAddress = rs.getString("ip_address");
  40. int siteId = rs.getInt("site_id");
  41.  
  42.  
  43. // -- ADD CODE HERE TO GIVE TOKENS OR WHATEVER
  44.  
  45.  
  46. System.out.println("[FoxVote] Vote claimed by "+name+". (sid: "+siteId+", ip: "+ipAddress+", time: "+timestamp+")");
  47.  
  48. rs.updateInt("claimed", 1); // do not delete otherwise they can reclaim!
  49. rs.updateRow();
  50. }
  51.  
  52. destroy();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57.  
  58.  
  59. public boolean connect(String host, String database, String user, String pass) {
  60. try {
  61. this.conn = DriverManager.getConnection("jdbc:mysql://"+host+":3306/"+database, user, pass);
  62. return true;
  63. } catch (SQLException e) {
  64. System.out.println("Failing connecting to database!");
  65. return false;
  66. }
  67. }
  68.  
  69. public void destroy() {
  70. try {
  71. conn.close();
  72. conn = null;
  73. if (stmt != null) {
  74. stmt.close();
  75. stmt = null;
  76. }
  77. } catch(Exception e) {
  78. e.printStackTrace();
  79. }
  80. }
  81.  
  82. public int executeUpdate(String query) {
  83. try {
  84. this.stmt = this.conn.createStatement(1005, 1008);
  85. int results = stmt.executeUpdate(query);
  86. return results;
  87. } catch (SQLException ex) {
  88. ex.printStackTrace();
  89. }
  90. return -1;
  91. }
  92.  
  93. public ResultSet executeQuery(String query) {
  94. try {
  95. this.stmt = this.conn.createStatement(1005, 1008);
  96. ResultSet results = stmt.executeQuery(query);
  97. return results;
  98. } catch (SQLException ex) {
  99. ex.printStackTrace();
  100. }
  101. return null;
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement