Advertisement
Guest User

Untitled

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