Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. package Myql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class MySQL {
  12.  
  13. private static final List<String> List = null;
  14. private String user, password, host, database, port;
  15. private Connection con;
  16. private Object DriverManger;
  17.  
  18. public void setCon(Connection con) {
  19. this.con = con;
  20. }
  21.  
  22. public MySQL(String user, String password, String host, String database, String port) {
  23.  
  24. this.user = user;
  25. this.password = password;
  26. this.host = host;
  27. this.database = database;
  28. this.port = port;
  29.  
  30. }
  31.  
  32. public void connect() {
  33.  
  34. if(isConnected()) {
  35. try {
  36.  
  37. setCon(DriverManager.getConnection("jdbc:msql://" + this.host + ":" + this.port));
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. } catch (SQLException exc) {
  45. exc.printStackTrace();
  46. }
  47.  
  48.  
  49. }
  50. }
  51.  
  52. public boolean isConnected() {
  53. return getCon() != null;
  54.  
  55. }
  56.  
  57. public void close() {
  58. if (isConnected()) {
  59. try {
  60.  
  61. getCon().close();
  62.  
  63. } catch (SQLException exc) {
  64. exc.printStackTrace();
  65. }
  66.  
  67. }
  68. }
  69.  
  70. public void update(String qry) {
  71. if(isConnected()) {
  72. try {
  73.  
  74. PreparedStatement ps = getCon().prepareStatement(qry);
  75. ps.executeUpdate();
  76.  
  77. } catch (SQLException exc) {
  78. exc.printStackTrace();
  79. }
  80. }
  81.  
  82.  
  83. }
  84.  
  85. public ResultSet query(String qry) {
  86. ResultSet rs = null;
  87. try {
  88.  
  89. PreparedStatement ps =getCon().prepareStatement(qry);
  90. rs = ps.executeQuery();
  91.  
  92.  
  93. } catch (SQLException exc) {
  94.  
  95.  
  96. } return rs;
  97.  
  98. }
  99.  
  100.  
  101. }
  102.  
  103. public void createTable() {
  104. update("CREATE TABLE IF NOT EXISTS Stats(UUID varchar(64), Points int, Kills int, Deaths int;)");
  105. }
  106.  
  107. public boolean isExists(String uuid) {
  108.  
  109. try {
  110.  
  111. ResultSet rs = query("SELECT * = FROM Stats WHERE UUID='" + uuid +"'");
  112. if(rs.next()) {
  113. return rs.getString("UUID") != null;
  114. }
  115. } catch (SQLException exc) {
  116.  
  117. }
  118.  
  119. return false;
  120. }
  121.  
  122. public void createPlayer(String uuid) {
  123. if(!isExists(uuid)) {
  124. update("INSERT INTO Stats(UUID, Points, Kills, Deaths) VALUES ('" + uuid + "', '0', '0', '0')");
  125. }
  126. }
  127.  
  128. public void setPoints(String uuid, int points) {
  129. if(!isExists(uuid)) {
  130. update("UPDATE Stats SET Points='" + points + "' WHERE UUID='" + uuid + "'");
  131. } else {
  132. createPlayer(uuid);
  133. setPoints(uuid, points);
  134. }
  135. }
  136.  
  137. public Integer getPoints(String uuid) {
  138. if(isExists(uuid)) {
  139. try {
  140. ResultSet rs = query("SELECT * FROM Stats WHERE UUID='" + uuid + "'");
  141. if (rs.next()) {
  142. return rs.getInt("Points");
  143. }
  144. } catch (SQLException exc) {
  145.  
  146. }
  147. } else {
  148. createPlayer(uuid);
  149. getPoints(uuid);
  150. }
  151. return 0;
  152. }
  153. public void setKills(String uuid, int kills) {
  154. if(isExists(uuid)) {
  155.  
  156. update("UPDATE Stats SET Kills='" + kills + "'WHERE UUID='" + "'");
  157.  
  158.  
  159. } else {
  160. createPlayer(uuid);
  161. setKills(uuid, kills);
  162. }
  163. }
  164.  
  165. public void setDeaths(String uuid, int deaths) {
  166. if(isExists(uuid)) {
  167.  
  168. update("UPDATE Stats SET DEATHS='" + deaths + "'WHERE UUID='" + "'");
  169.  
  170. } else {
  171. createPlayer(uuid);
  172. setKills(uuid, deaths);
  173. }
  174. }
  175.  
  176. public Integer getKills(String uuid) {
  177. Integer i = Integer.valueOf(0);
  178. if(isExists(uuid)) {
  179. ResultSet rs =query("Select * FROM Stats WHERE UUID='" + uuid + "'");
  180.  
  181. try {
  182. if(rs.next()) {
  183. i = rs.getInt("Kills");
  184. }
  185. } catch (SQLException e) {
  186. e.printStackTrace();
  187. }
  188.  
  189. } else {
  190. createPlayer(uuid);
  191. getKills(uuid);
  192. }
  193. return i;
  194. }
  195.  
  196. public Integer getDeaths(String uuid) {
  197. int i = Integer.valueOf(0);
  198. if(isExists(uuid)) {
  199. ResultSet rs = query("SELECT * FROM Stats WHERE UUID'" + uuid + "'");
  200. try {
  201. if(rs.next()) {
  202. i = rs.getInt("Deaths");
  203. }
  204. } catch (SQLException e) {
  205. e.printStackTrace();
  206. }
  207. } else {
  208. createPlayer(uuid);
  209. getDeaths(uuid);
  210. }
  211. return i;
  212. }
  213.  
  214. public void addPoints(String uuid, int points) {
  215. if(isExists(uuid)) {
  216. setPoints(uuid, getPoints(uuid) + points);
  217.  
  218. } else {
  219. createPlayer(uuid);
  220. addPoints(uuid, points);
  221. }
  222. }
  223.  
  224. public void removePoints(String uuid, int points) {
  225. if(isExists(uuid)) {
  226. removePoints(uuid, getPoints(uuid) + points);
  227.  
  228. } else {
  229. createPlayer(uuid);
  230. addPoints(uuid, points);
  231. }
  232. }
  233.  
  234. public void addKills(String uuid, int kills) {
  235. if(isExists(uuid)) {
  236. setKills(uuid, getPoints(uuid) + kills);
  237.  
  238. } else {
  239. createPlayer(uuid);
  240. addKills(uuid, kills);
  241. }
  242. }
  243.  
  244. public void addDeaths(String uuid, int deaths) {
  245. if(isExists(uuid)) {
  246. setDeaths(uuid, getPoints(uuid) + deaths);
  247.  
  248. } else {
  249. createPlayer(uuid);
  250. addDeaths(uuid, deaths);
  251.  
  252. }
  253. }
  254.  
  255.  
  256. public void removeKills(String uuid, int kills) {
  257. if(isExists(uuid)) {
  258. setKills(uuid, getPoints(uuid) + kills);
  259.  
  260. } else {
  261. createPlayer(uuid);
  262. removeKills(uuid, kills);
  263.  
  264. }
  265.  
  266. }
  267.  
  268. public void removeDeaths(String uuid, int deaths) {
  269. if(isExists(uuid)) {
  270. setDeaths(uuid, getPoints(uuid) + deaths);
  271.  
  272. } else {
  273. createPlayer(uuid);
  274. removeDeaths(uuid, deaths);
  275.  
  276. }
  277.  
  278. }
  279.  
  280. public int getRank(String uuid) {
  281.  
  282. int rank = -1;
  283.  
  284. try {
  285.  
  286. ResultSet rs = query("SELECT * FROM Stats ODER BY POINTS DESC");
  287. while(rs.next()) {
  288. String uuid2 = rs.getString("UUID");
  289. if(uuid2.equalsIgnoreCase(uuid)) {
  290. rank = rs.getRow();
  291. break;
  292. }
  293. }
  294. } catch (SQLException exc) {
  295.  
  296. }
  297. return rank;
  298.  
  299.  
  300. }
  301.  
  302. public String getUUID(int rank) {
  303. try {
  304. int nrank = rank -1;
  305. ResultSet rs = query("SELECT * FROM Stats ORDER BY POINTS DESC LIMIT" + nrank + ", " + rank);
  306. if (rs.next()) {
  307. return rs.getString("UUID");
  308. }
  309.  
  310. } catch (SQLException exc) {
  311.  
  312. }
  313. return null;
  314.  
  315. }
  316.  
  317. public List<String> getTopTen() {
  318.  
  319. List<String> list = new ArrayList<>();
  320. try {
  321.  
  322. ResultSet rs = query("SELECT * FROM Stats ORDER BY POINTS DESC");
  323. while(rs.next()) {
  324. list.add(rs.getString("UUID"));
  325. }
  326. } catch (SQLException exc) {
  327.  
  328. }
  329. return List;
  330.  
  331.  
  332. }
  333.  
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement