Advertisement
Guest User

Untitled

a guest
Apr 11th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. package Donate;
  2.  
  3. import cn.nukkit.command.ConsoleCommandSender;
  4. import cn.nukkit.utils.Config;
  5.  
  6. import java.sql.*;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class SQL {
  11.  
  12. private final Thread executor = new Thread(new SQLExecutor());
  13.  
  14. private final String url;
  15. private final String username;
  16. private final String password;
  17.  
  18. private Config config;
  19. private boolean killed;
  20. private String query;
  21.  
  22. public SQL() {
  23. this.url = "jdbc:mysql://" + this.config.getString("sql.host") + ":" + this.config.getString("sql.port") + "/" + this.config.getString("sql.db");
  24. this.username = this.config.getString("sql.user");
  25. this.password = this.config.getString("sql.pass");
  26. this.killed = false;
  27. }
  28.  
  29. public ResultSet executeSelect(String query) throws SQLException {
  30. List<String> list = new ArrayList<String>();
  31. Connection connection = DriverManager.getConnection(SQL.this.url, SQL.this.username, SQL.this.password);
  32. if (connection == null) return null;
  33. Statement statement = connection.prepareStatement(query);
  34. if (statement == null) return null;
  35. ResultSet resultSet = statement.executeQuery(query);
  36. statement.close();
  37. connection.close();
  38. return resultSet;
  39. }
  40.  
  41. public void executeUpdate (String query) throws SQLException {
  42. Connection connection = DriverManager.getConnection(SQL.this.url, SQL.this.username, SQL.this.password);
  43. if (connection == null) return;
  44. Statement statement = connection.prepareStatement(query);
  45. statement.executeUpdate(query);
  46. statement.close();
  47. connection.close();
  48. }
  49.  
  50.  
  51.  
  52. public void check(String name) throws SQLException {
  53. ResultSet result = executeSelect("SELECT * FROM permissions WHERE name='" + name + "' ORDER BY id DESC");
  54. if (result == null) return;
  55. Donate.getInstance().getLogger().info(name + " Entered!");
  56. while (result.next()){
  57. long date = result.getLong("date"); // если я правильно понимаю, колонка у вас называется date
  58. if (System.currentTimeMillis()>date) {
  59. Donate.getInstance().getLogger().info(name + " deop!");
  60. executeUpdate("DELETE FROM permissions WHERE name='" + name + "'");
  61. Donate.getInstance().getServer().dispatchCommand(new ConsoleCommandSender(), "setgroup player " + name);
  62. } else {
  63. Donate.getInstance().getServer().dispatchCommand(new ConsoleCommandSender(), "setgroup player "+result.getString("permission"));
  64. }
  65. }
  66. }
  67.  
  68. public boolean isKilled() {
  69. return this.killed;
  70. }
  71.  
  72. private class SQLExecutor implements Runnable {
  73. @Override
  74. public void run() {
  75. try {
  76. Connection conn = DriverManager.getConnection(SQL.this.url, SQL.this.username, SQL.this.password);
  77. PreparedStatement statement = conn.prepareStatement(SQL.this.query);
  78. statement.executeUpdate();
  79. } catch (SQLException e) {
  80. Donate.getInstance().getLogger().error("SQL Error: " + e);
  81. SQL.this.killed = true;
  82. }
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement