Advertisement
Guest User

Untitled

a guest
May 4th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. package pl.mcorbita.topki.mysql;
  2.  
  3. import org.bukkit.plugin.*;
  4.  
  5. import pl.mcorbita.topki.Main;
  6. import pl.mcorbita.topki.objects.User;
  7. import pl.mcorbita.topki.objects.UserManager;
  8. import pl.mcorbita.topki.utils.TopType;
  9. import pl.mcorbita.topki.utils.Utils;
  10.  
  11. import java.sql.*;
  12. import java.util.*;
  13.  
  14. public class MySQL
  15. {
  16. private String host;
  17. private int port;
  18. private String user;
  19. private String pass;
  20. private String database;
  21. private String url;
  22. private Connection conn;
  23.  
  24. public MySQL(final String host, final int port, final String user, final String pass, final String database) {
  25. this.host = host;
  26. this.port = port;
  27. this.user = user;
  28. this.pass = pass;
  29. this.database = database;
  30. this.buildURL();
  31. this.connect();
  32. }
  33.  
  34. public void buildURL() {
  35. this.url = "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true";
  36. }
  37.  
  38. public boolean isConnected() {
  39. return this.conn != null;
  40. }
  41.  
  42. public boolean connect() {
  43. try {
  44. Utils.sendMsgToConsole((Plugin)Main.getInst(), "&aUsing url to connect: " + this.url);
  45. this.conn = DriverManager.getConnection(this.url, this.user, this.pass);
  46. Utils.sendMsgToConsole((Plugin)Main.getInst(), "&aPolaczono z baza danych MySQL!");
  47. return true;
  48. }
  49. catch (SQLException e) {
  50. Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
  51. return false;
  52. }
  53. }
  54.  
  55. public void createUsersFromMySQL() {
  56. try {
  57. int i = 0;
  58. final ResultSet rs = this.query("SELECT * FROM `tops`");
  59. while (rs.next()) {
  60. final UUID uuid = UUID.fromString(rs.getString("uuid"));
  61. final String name = rs.getString("name");
  62. final int kills = rs.getInt("kills");
  63. final int kox = rs.getInt("kox");
  64. final int ref = rs.getInt("ref");
  65. final int breakedBlocks = rs.getInt("breakedBlocks");
  66. final int placedBlocks = rs.getInt("placedBlocks");
  67. final int obsidian = rs.getInt("obsidian");
  68. final long time = rs.getLong("time");
  69. final int money = rs.getInt("money");
  70. UserManager.createUser(uuid, name, kills, kox, ref, breakedBlocks, placedBlocks, obsidian, time, money);
  71. ++i;
  72. }
  73. Utils.sendMsgToConsole((Plugin)Main.getInst(), "&aZaladowany: &c" + i + " &agraczy z bazy danych!");
  74. }
  75. catch (SQLException e) {
  76. Utils.sendMsgToConsole((Plugin)Main.getInst(), e.getMessage());
  77. }
  78. }
  79.  
  80. public void send(final String sql) {
  81. if (!this.isConnected()) {
  82. this.connect();
  83. }
  84. PreparedStatement ps;
  85. final Thread t = new Thread(() -> {
  86. try {
  87. ps = this.conn.prepareStatement(sql);
  88. ps.execute();
  89. }
  90. catch (SQLException e) {
  91. Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
  92. e.printStackTrace();
  93. }
  94. return;
  95. });
  96. t.start();
  97. }
  98.  
  99. public ResultSet query(final String sql) {
  100. if (!this.isConnected()) {
  101. this.connect();
  102. }
  103. try {
  104. return this.conn.createStatement().executeQuery(sql);
  105. }
  106. catch (SQLException e) {
  107. Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
  108. return null;
  109. }
  110. }
  111.  
  112. public Object get(final String tableName, final String columnName, final UUID uuid) {
  113. try {
  114. final PreparedStatement ps = this.conn.prepareStatement("SELECT * FROM `" + tableName + "` WHERE uuid = ?");
  115. ps.setString(1, uuid.toString());
  116. final ResultSet rs = ps.executeQuery();
  117. rs.last();
  118. if (rs.getRow() != 0) {
  119. rs.first();
  120. return rs.getObject(columnName);
  121. }
  122. }
  123. catch (SQLException e) {
  124. Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
  125. }
  126. return null;
  127. }
  128.  
  129. public void set(final String tableName, final String columnName, final Object newValue, final UUID uuid) {
  130. final Thread t = new Thread() {
  131. @Override
  132. public void run() {
  133. try {
  134. final PreparedStatement ps = MySQL.this.conn.prepareStatement("UPDATE `" + tableName + "` SET `" + columnName + "`='" + newValue + "' WHERE uuid = ?");
  135. ps.setString(1, uuid.toString());
  136. ps.executeUpdate();
  137. ps.close();
  138. }
  139. catch (SQLException e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. };
  144. t.start();
  145. }
  146.  
  147. public List<User> getTop(final TopType type) {
  148. final List<User> users = new ArrayList<User>();
  149. try {
  150. final PreparedStatement ps = this.conn.prepareStatement("SELECT * FROM `tops` ORDER BY " + TopType.getMySQLType(type) + " DESC LIMIT 10;");
  151. final ResultSet rs = ps.executeQuery();
  152. while (rs.next()) {
  153. users.add(UserManager.getUser(rs.getString("name")));
  154. }
  155. }
  156. catch (SQLException e) {
  157. Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
  158. }
  159. return users;
  160. }
  161.  
  162. public void saveDefaultData(final User u) {
  163. this.send("INSERT INTO `tops` (uuid, name, kills, kox, ref, breakedBlocks, placedBlocks, obsidian, time, money) VALUES ('" + u.getUuid() + "', '" + u.getName() + "', '0', '0', '0', '0', '0', '0', '0', '0')");
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement