Advertisement
maxmurks

Untitled

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