Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.10 KB | None | 0 0
  1. package de.uhcsg.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.UUID;
  9. import org.bukkit.entity.Player;
  10.  
  11. import de.uhcsg.main.Main;
  12.  
  13. public class MySQL
  14. {
  15. private static String host = "";
  16. private static String database = "";
  17. private static String user = "";
  18. private static String password = "";
  19.  
  20. public MySQL() {
  21.  
  22. connect();
  23. }
  24.  
  25. public MySQL(String host, String databse, String user) {
  26. this.host = host;
  27. this.database = database;
  28. this.user = user;
  29.  
  30. connect();
  31. }
  32.  
  33. private static Connection connection;
  34.  
  35. public static Connection getConnection()
  36. {
  37. return connection;
  38. }
  39.  
  40. public static boolean isConnected()
  41. {
  42. return connection != null;
  43. }
  44.  
  45. public static void connect()
  46. {
  47. if (!isConnected()) {
  48. try
  49. {
  50. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database + "?autoReconnect=true", user, password);
  51. }
  52. catch (SQLException ex)
  53. {
  54. ex.printStackTrace();
  55. }
  56. }
  57. }
  58.  
  59. public static void disconnect()
  60. {
  61. if (isConnected()) {
  62. try
  63. {
  64. connection.close();
  65. }
  66. catch (SQLException ex)
  67. {
  68. ex.printStackTrace();
  69. }
  70. }
  71. }
  72.  
  73. public static void createTableIfNotExists()
  74. {
  75. if (isConnected()) {
  76. try
  77. {
  78. PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Uhcsg (Spielername VARCHAR(100), UUID VARCHAR(100), Kills INT(100), Tode INT(100), Siege INT(100), Niederlagen INT(100), Unentschieden INT(100))");
  79. ps.executeUpdate();
  80. ps.close();
  81. }
  82. catch (SQLException ex)
  83. {
  84. ex.printStackTrace();
  85. }
  86. }
  87. }
  88.  
  89. public static boolean isUserExisting(Player p)
  90. {
  91. try
  92. {
  93. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  94. ps.setString(1, p.getUniqueId().toString());
  95. ResultSet result = ps.executeQuery();
  96. boolean user = result.next();
  97. result.close();
  98. ps.close();
  99. return user;
  100. }
  101. catch (Exception ex)
  102. {
  103. ex.printStackTrace();
  104. }
  105. return false;
  106. }
  107.  
  108. public static boolean isUserExisting(UUID uuid)
  109. {
  110. try
  111. {
  112. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  113. ps.setString(1, uuid.toString());
  114. ResultSet result = ps.executeQuery();
  115. boolean user = result.next();
  116. result.close();
  117. ps.close();
  118. return user;
  119. }
  120. catch (Exception ex)
  121. {
  122. ex.printStackTrace();
  123. }
  124. return false;
  125. }
  126.  
  127. public static void registerPlayer(Player p)
  128. {
  129. if (isUserExisting(p)) {
  130. return;
  131. }
  132. try
  133. {
  134. PreparedStatement ps = connection.prepareStatement("INSERT INTO Uhcsg (Spielername, UUID, Kills, Tode, Siege, Niederlagen, Unentschieden) VALUES (?, ?, ?, ?, ?, ?, ?)");
  135. ps.setString(1, p.getName());
  136. ps.setString(2, p.getUniqueId().toString());
  137. ps.setInt(3, 0);
  138. ps.setInt(4, 0);
  139. ps.setInt(5, 0);
  140. ps.setInt(6, 0);
  141. ps.setInt(7, 0);
  142. ps.execute();
  143. ps.close();
  144. }
  145. catch (Exception ex)
  146. {
  147. ex.printStackTrace();
  148. }
  149. }
  150.  
  151. public static void setPlayerName(Player p)
  152. {
  153. try
  154. {
  155. PreparedStatement ps = connection.prepareStatement("UPDATE Uhcsg SET Spielername = ? WHERE UUID = ?");
  156. ps.setString(1, p.getName());
  157. ps.setString(2, p.getUniqueId().toString());
  158. ps.executeUpdate();
  159. ps.close();
  160. }
  161. catch (Exception ex)
  162. {
  163. ex.printStackTrace();
  164. }
  165. }
  166.  
  167. public static String getPlayer(UUID uuid)
  168. {
  169. try
  170. {
  171. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  172. ps.setString(1, uuid.toString());
  173. ResultSet result = ps.executeQuery();
  174. result.next();
  175. String name = result.getString("Spielername");
  176. result.close();
  177. ps.close();
  178. return name;
  179. }
  180. catch (Exception ex)
  181. {
  182. ex.printStackTrace();
  183. }
  184. return "null";
  185. }
  186.  
  187. public static int getKills(Player p)
  188. {
  189. try
  190. {
  191. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  192. ps.setString(1, p.getUniqueId().toString());
  193. ResultSet result = ps.executeQuery();
  194. result.next();
  195. int kills = result.getInt("Kills");
  196. result.close();
  197. ps.close();
  198. return kills;
  199. }
  200. catch (Exception ex)
  201. {
  202. ex.printStackTrace();
  203. }
  204. return -1;
  205. }
  206.  
  207. public static int getKills(UUID uuid)
  208. {
  209. try
  210. {
  211. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  212. ps.setString(1, uuid.toString());
  213. ResultSet result = ps.executeQuery();
  214. result.next();
  215. int kills = result.getInt("Kills");
  216. result.close();
  217. ps.close();
  218. return kills;
  219. }
  220. catch (Exception ex)
  221. {
  222. ex.printStackTrace();
  223. }
  224. return -1;
  225. }
  226.  
  227. public static int getDeaths(Player p)
  228. {
  229. try
  230. {
  231. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  232. ps.setString(1, p.getUniqueId().toString());
  233. ResultSet result = ps.executeQuery();
  234. result.next();
  235. int deaths = result.getInt("Tode");
  236. result.close();
  237. ps.close();
  238. return deaths;
  239. }
  240. catch (Exception ex)
  241. {
  242. ex.printStackTrace();
  243. }
  244. return -1;
  245. }
  246.  
  247. public static int getDeaths(UUID uuid)
  248. {
  249. try
  250. {
  251. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  252. ps.setString(1, uuid.toString());
  253. ResultSet result = ps.executeQuery();
  254. result.next();
  255. int deaths = result.getInt("Tode");
  256. result.close();
  257. ps.close();
  258. return deaths;
  259. }
  260. catch (Exception ex)
  261. {
  262. ex.printStackTrace();
  263. }
  264. return -1;
  265. }
  266.  
  267. public static double getKD(Player p)
  268. {
  269. try
  270. {
  271. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  272. ps1.setString(1, p.getUniqueId().toString());
  273. ResultSet result1 = ps1.executeQuery();
  274. result1.next();
  275. double kills = result1.getInt("Kills");
  276. result1.close();
  277. ps1.close();
  278. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  279. ps2.setString(1, p.getUniqueId().toString());
  280. ResultSet result2 = ps2.executeQuery();
  281. result2.next();
  282. double deaths = result2.getInt("Tode");
  283. result2.close();
  284. ps2.close();
  285. if ((kills == 0.0D) && (deaths == 0.0D)) {
  286. return 0.0D;
  287. }
  288. if (deaths == 0.0D) {
  289. return kills;
  290. }
  291. return Math.round(kills / deaths * 100.0D) / 100.0D;
  292. }
  293. catch (Exception ex)
  294. {
  295. ex.printStackTrace();
  296. }
  297. return -1.0D;
  298. }
  299.  
  300. public static double getKD(UUID uuid)
  301. {
  302. try
  303. {
  304. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  305. ps1.setString(1, uuid.toString());
  306. ResultSet result1 = ps1.executeQuery();
  307. result1.next();
  308. double kills = result1.getInt("Kills");
  309. result1.close();
  310. ps1.close();
  311. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  312. ps2.setString(1, uuid.toString());
  313. ResultSet result2 = ps2.executeQuery();
  314. result2.next();
  315. double deaths = result2.getInt("Tode");
  316. result2.close();
  317. ps2.close();
  318. if ((kills == 0.0D) && (deaths == 0.0D)) {
  319. return 0.0D;
  320. }
  321. if (deaths == 0.0D) {
  322. return kills;
  323. }
  324. return Math.round(kills / deaths * 100.0D) / 100.0D;
  325. }
  326. catch (Exception ex)
  327. {
  328. ex.printStackTrace();
  329. }
  330. return -1.0D;
  331. }
  332.  
  333. public static int getGamesPlayed(Player p)
  334. {
  335. try
  336. {
  337. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  338. ps1.setString(1, p.getUniqueId().toString());
  339. ResultSet result1 = ps1.executeQuery();
  340. result1.next();
  341. int wins = result1.getInt("Siege");
  342. result1.close();
  343. ps1.close();
  344. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  345. ps2.setString(1, p.getUniqueId().toString());
  346. ResultSet result2 = ps2.executeQuery();
  347. result2.next();
  348. int looses = result2.getInt("Niederlagen");
  349. result2.close();
  350. ps2.close();
  351. PreparedStatement ps3 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  352. ps3.setString(1, p.getUniqueId().toString());
  353. ResultSet result3 = ps3.executeQuery();
  354. result3.next();
  355. int undecideds = result3.getInt("Unentschieden");
  356. result3.close();
  357. ps3.close();
  358. return wins + looses + undecideds;
  359. }
  360. catch (Exception ex)
  361. {
  362. ex.printStackTrace();
  363. }
  364. return -1;
  365. }
  366.  
  367. public static int getGamesPlayed(UUID uuid)
  368. {
  369. try
  370. {
  371. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  372. ps1.setString(1, uuid.toString());
  373. ResultSet result1 = ps1.executeQuery();
  374. result1.next();
  375. int wins = result1.getInt("Siege");
  376. result1.close();
  377. ps1.close();
  378. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  379. ps2.setString(1, uuid.toString());
  380. ResultSet result2 = ps2.executeQuery();
  381. result2.next();
  382. int looses = result2.getInt("Niederlagen");
  383. result2.close();
  384. ps2.close();
  385. PreparedStatement ps3 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  386. ps3.setString(1, uuid.toString());
  387. ResultSet result3 = ps3.executeQuery();
  388. result3.next();
  389. int undecideds = result3.getInt("Unentschieden");
  390. result3.close();
  391. ps3.close();
  392. return wins + looses + undecideds;
  393. }
  394. catch (Exception ex)
  395. {
  396. ex.printStackTrace();
  397. }
  398. return -1;
  399. }
  400.  
  401. public static int getWins(Player p)
  402. {
  403. try
  404. {
  405. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  406. ps.setString(1, p.getUniqueId().toString());
  407. ResultSet result = ps.executeQuery();
  408. result.next();
  409. int wins = result.getInt("Siege");
  410. result.close();
  411. ps.close();
  412. return wins;
  413. }
  414. catch (Exception ex)
  415. {
  416. ex.printStackTrace();
  417. }
  418. return -1;
  419. }
  420.  
  421. public static int getWins(UUID uuid)
  422. {
  423. try
  424. {
  425. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  426. ps.setString(1, uuid.toString());
  427. ResultSet result = ps.executeQuery();
  428. result.next();
  429. int wins = result.getInt("Siege");
  430. result.close();
  431. ps.close();
  432. return wins;
  433. }
  434. catch (Exception ex)
  435. {
  436. ex.printStackTrace();
  437. }
  438. return -1;
  439. }
  440.  
  441. public static int getLooses(Player p)
  442. {
  443. try
  444. {
  445. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  446. ps.setString(1, p.getUniqueId().toString());
  447. ResultSet result = ps.executeQuery();
  448. result.next();
  449. int looses = result.getInt("Niederlagen");
  450. result.close();
  451. ps.close();
  452. return looses;
  453. }
  454. catch (Exception ex)
  455. {
  456. ex.printStackTrace();
  457. }
  458. return -1;
  459. }
  460.  
  461. public static int getLooses(UUID uuid)
  462. {
  463. try
  464. {
  465. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  466. ps.setString(1, uuid.toString());
  467. ResultSet result = ps.executeQuery();
  468. result.next();
  469. int looses = result.getInt("Niederlagen");
  470. result.close();
  471. ps.close();
  472. return looses;
  473. }
  474. catch (Exception ex)
  475. {
  476. ex.printStackTrace();
  477. }
  478. return -1;
  479. }
  480.  
  481. public static int getUndecideds(Player p)
  482. {
  483. try
  484. {
  485. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  486. ps.setString(1, p.getUniqueId().toString());
  487. ResultSet result = ps.executeQuery();
  488. result.next();
  489. int wins = result.getInt("Unentschieden");
  490. result.close();
  491. ps.close();
  492. return wins;
  493. }
  494. catch (Exception ex)
  495. {
  496. ex.printStackTrace();
  497. }
  498. return -1;
  499. }
  500.  
  501. public static int getUndecideds(UUID uuid)
  502. {
  503. try
  504. {
  505. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  506. ps.setString(1, uuid.toString());
  507. ResultSet result = ps.executeQuery();
  508. result.next();
  509. int wins = result.getInt("Unentschieden");
  510. result.close();
  511. ps.close();
  512. return wins;
  513. }
  514. catch (Exception ex)
  515. {
  516. ex.printStackTrace();
  517. }
  518. return -1;
  519. }
  520.  
  521. public static double getWL(Player p)
  522. {
  523. try
  524. {
  525. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  526. ps1.setString(1, p.getUniqueId().toString());
  527. ResultSet result1 = ps1.executeQuery();
  528. result1.next();
  529. double wins = result1.getInt("Siege");
  530. result1.close();
  531. ps1.close();
  532. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  533. ps2.setString(1, p.getUniqueId().toString());
  534. ResultSet result2 = ps2.executeQuery();
  535. result2.next();
  536. double looses = result2.getInt("Niederlagen");
  537. result2.close();
  538. ps2.close();
  539. if ((wins == 0.0D) && (looses == 0.0D)) {
  540. return 0.0D;
  541. }
  542. if (looses == 0.0D) {
  543. return wins;
  544. }
  545. return Math.round(wins / looses * 100.0D) / 100.0D;
  546. }
  547. catch (Exception ex)
  548. {
  549. ex.printStackTrace();
  550. }
  551. return -1.0D;
  552. }
  553.  
  554. public static double getWL(UUID uuid)
  555. {
  556. try
  557. {
  558. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  559. ps1.setString(1, uuid.toString());
  560. ResultSet result1 = ps1.executeQuery();
  561. result1.next();
  562. double wins = result1.getInt("Siege");
  563. result1.close();
  564. ps1.close();
  565. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM Uhcsg WHERE UUID = ?");
  566. ps2.setString(1, uuid.toString());
  567. ResultSet result2 = ps2.executeQuery();
  568. result2.next();
  569. double looses = result2.getInt("Niederlagen");
  570. result2.close();
  571. ps2.close();
  572. if ((wins == 0.0D) && (looses == 0.0D)) {
  573. return 0.0D;
  574. }
  575. if (looses == 0.0D) {
  576. return wins;
  577. }
  578. return Math.round(wins / looses * 100.0D) / 100.0D;
  579. }
  580. catch (Exception ex)
  581. {
  582. ex.printStackTrace();
  583. }
  584. return -1.0D;
  585. }
  586.  
  587. public static int getRank(Player p)
  588. {
  589. int rank = -1;
  590. try
  591. {
  592. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg ORDER BY Siege DESC");
  593. ResultSet result = ps.executeQuery();
  594. while (result.next())
  595. {
  596. String uuid2 = result.getString("UUID");
  597. if (uuid2.equalsIgnoreCase(p.getUniqueId().toString()))
  598. {
  599. rank = result.getRow();
  600. break;
  601. }
  602. }
  603. result.close();
  604. ps.close();
  605. }
  606. catch (Exception ex)
  607. {
  608. ex.printStackTrace();
  609. }
  610. return rank;
  611. }
  612.  
  613. public static int getRank(UUID uuid)
  614. {
  615. int rank = -1;
  616. try
  617. {
  618. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Uhcsg ORDER BY Siege DESC");
  619. ResultSet result = ps.executeQuery();
  620. while (result.next())
  621. {
  622. String uuid2 = result.getString("UUID");
  623. if (uuid2.equalsIgnoreCase(uuid.toString()))
  624. {
  625. rank = result.getRow();
  626. break;
  627. }
  628. }
  629. result.close();
  630. ps.close();
  631. }
  632. catch (Exception ex)
  633. {
  634. ex.printStackTrace();
  635. }
  636. return rank;
  637. }
  638.  
  639. public static void addKill(Player p)
  640. {
  641. try
  642. {
  643. PreparedStatement ps = connection.prepareStatement("UPDATE Uhcsg SET Kills = ? WHERE uuid = ?");
  644. ps.setInt(1, getKills(p) + 1);
  645. ps.setString(2, p.getUniqueId().toString());
  646. ps.executeUpdate();
  647. ps.close();
  648. }
  649. catch (Exception ex)
  650. {
  651. ex.printStackTrace();
  652. }
  653. }
  654.  
  655. public static void addDeath(Player p)
  656. {
  657. try
  658. {
  659. PreparedStatement ps = connection.prepareStatement("UPDATE Uhcsg SET Tode = ? WHERE uuid = ?");
  660. ps.setInt(1, getDeaths(p) + 1);
  661. ps.setString(2, p.getUniqueId().toString());
  662. ps.executeUpdate();
  663. ps.close();
  664. }
  665. catch (Exception ex)
  666. {
  667. ex.printStackTrace();
  668. }
  669. }
  670.  
  671. public static void addWin(Player p)
  672. {
  673. try
  674. {
  675. PreparedStatement ps = connection.prepareStatement("UPDATE Uhcsg SET Siege = ? WHERE uuid = ?");
  676. ps.setInt(1, getWins(p) + 1);
  677. ps.setString(2, p.getUniqueId().toString());
  678. ps.executeUpdate();
  679. ps.close();
  680. }
  681. catch (Exception ex)
  682. {
  683. ex.printStackTrace();
  684. }
  685. }
  686.  
  687. public static void addLoose(Player p)
  688. {
  689. try
  690. {
  691. PreparedStatement ps = connection.prepareStatement("UPDATE Uhcsg SET Niederlagen = ? WHERE uuid = ?");
  692. ps.setInt(1, getLooses(p) + 1);
  693. ps.setString(2, p.getUniqueId().toString());
  694. ps.executeUpdate();
  695. ps.close();
  696. }
  697. catch (Exception ex)
  698. {
  699. ex.printStackTrace();
  700. }
  701. }
  702.  
  703. public static void addUndecided(Player p)
  704. {
  705. try
  706. {
  707. PreparedStatement ps = connection.prepareStatement("UPDATE Uhcsg SET Unentschieden = ? WHERE uuid = ?");
  708. ps.setInt(1, getUndecideds(p) + 1);
  709. ps.setString(2, p.getUniqueId().toString());
  710. ps.executeUpdate();
  711. ps.close();
  712. }
  713. catch (Exception ex)
  714. {
  715. ex.printStackTrace();
  716. }
  717. }
  718. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement