Advertisement
MrObst

MySQL

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