Advertisement
Guest User

Untitled

a guest
Jan 7th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. package de.gg.utils;
  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.  
  9. public class MySQL {
  10.  
  11. public static Connection con;
  12.  
  13. public static Connection getCon() {
  14. return con;
  15. }
  16.  
  17. public static void connect() {
  18.  
  19. try {
  20. con = DriverManager.getConnection(
  21. "jdbc:mysql://" + CFGManager.MYSQLcfg.getString("HOST") + ":" + CFGManager.MYSQLcfg.getInt("PORT")
  22. + "/" + CFGManager.MYSQLcfg.getString("DATABASE") + "?autoReconnect=true",
  23. CFGManager.MYSQLcfg.getString("USER"), CFGManager.MYSQLcfg.getString("PASSWORD"));
  24.  
  25. createTable();
  26. createNickTable();
  27. System.out.println("§7[§cMySQL§7] §cVerbindung hergestellt!");
  28. } catch (SQLException e) {
  29. System.out.println("§7[§cMySQL§7] §cDie Verbindung ist fehlgeschlagen! Error: " + e.getMessage());
  30. }
  31.  
  32. }
  33.  
  34. public static boolean nick(String uuid) {
  35.  
  36. boolean s = false;
  37.  
  38. if (isConnected()) {
  39.  
  40. ResultSet rs = query("SELECT * FROM Nick WHERE UUID= '" + uuid + "'");
  41.  
  42. try {
  43.  
  44. if (rs.getString("joinNicked") == "true") {
  45. s = true;
  46. }
  47. } catch (SQLException e) {
  48. }
  49.  
  50. }
  51. return s;
  52. }
  53.  
  54. public static void resetnick(String uuid) {
  55.  
  56. if (isConnected()) {
  57.  
  58. if (playerExist(uuid)) {
  59.  
  60. update("UPDATE Nick SET Nickname= '' WHERE UUID= '" + uuid + "';");
  61. update("UPDATE Nick SET isNicked= 'false' WHERE UUID= '" + uuid + "';");
  62. } else {
  63. createPlayer(uuid);
  64. }
  65. }
  66. }
  67.  
  68. public static boolean isConnected() {
  69.  
  70. return getCon() != null;
  71. }
  72.  
  73. public static void update(String qry) {
  74. if (isConnected()) {
  75.  
  76. try {
  77.  
  78. PreparedStatement ps = getCon().prepareStatement(qry);
  79. ps.executeUpdate();
  80.  
  81. } catch (SQLException exc) {
  82. exc.printStackTrace();
  83. }
  84. }
  85. }
  86.  
  87. public static ResultSet query(String qry) {
  88. ResultSet rs;
  89.  
  90. try {
  91.  
  92. PreparedStatement ps = getCon().prepareStatement(qry);
  93. rs = ps.executeQuery();
  94. return rs;
  95.  
  96. } catch (SQLException exc) {
  97.  
  98. }
  99. return null;
  100. }
  101.  
  102. public static void close() {
  103. if (isConnected()) {
  104.  
  105. try {
  106.  
  107. getCon().close();
  108.  
  109. } catch (SQLException exc) {
  110. exc.printStackTrace();
  111. }
  112. }
  113. }
  114.  
  115. public static void createTable() {
  116. if (isConnected()) {
  117. update("CREATE TABLE IF NOT EXISTS GunGame(UUID varchar(64), Kills int, Deaths int, Level int)");
  118. }
  119. }
  120.  
  121. public static void createNickTable() {
  122. if (isConnected()) {
  123.  
  124. update("CREATE TABLE IF NOT EXISTS Nick (UUID VARCHAR(64), joinNicked VARCHAR(10), Nickname VARCHAR(16), isNicked VARCHAR(10)");
  125. }
  126. }
  127.  
  128. public static boolean playerExist(String uuid) {
  129.  
  130. if (isConnected()) {
  131.  
  132. try {
  133.  
  134. if (con != null) {
  135.  
  136. ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
  137.  
  138. if (rs.next()) {
  139.  
  140. return rs.getString("UUID") != null;
  141. }
  142. }
  143. } catch (SQLException exc) {
  144.  
  145. exc.printStackTrace();
  146. }
  147. }
  148. return false;
  149. }
  150.  
  151. public static void createPlayer(String uuid) {
  152.  
  153. if (isConnected()) {
  154.  
  155. if (!playerExist(uuid)) {
  156.  
  157. update("INSERT INTO GunGame(UUID, Kills, Deaths, Level) VALUES ('" + uuid + "', '0', '0', '0');");
  158. }
  159. }
  160.  
  161. }
  162.  
  163. public static void addKill(String uuid) {
  164.  
  165. if (isConnected()) {
  166.  
  167. if (playerExist(uuid)) {
  168.  
  169. ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
  170.  
  171. try {
  172. update("UPDATE GunGame SET Kills= '" + rs.getInt("Kills") + 1 + "' WHERE UUID= '" + uuid + "';");
  173.  
  174. } catch (SQLException e) {
  175. }
  176. } else {
  177. createPlayer(uuid);
  178. }
  179. }
  180.  
  181. }
  182.  
  183. public static void addDeath(String uuid) {
  184.  
  185. if (isConnected()) {
  186.  
  187. if (playerExist(uuid)) {
  188.  
  189. ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
  190.  
  191. try {
  192. update("UPDATE GunGame SET Deaths= '" + rs.getInt("Deaths") + 1 + "' WHERE UUID= '" + uuid + "';");
  193.  
  194. } catch (SQLException e) {
  195. }
  196. } else {
  197. createPlayer(uuid);
  198. }
  199. }
  200.  
  201. }
  202.  
  203. public static void removePlayer(String uuid) {
  204.  
  205. if (isConnected()) {
  206.  
  207. if (playerExist(uuid)) {
  208.  
  209. update("DELETE * FROM GunGame WHERE UUID= '" + uuid + "'");
  210. }
  211. }
  212.  
  213. }
  214.  
  215. public static int getKills(String uuid) {
  216.  
  217. int i = 0;
  218.  
  219. if (isConnected()) {
  220.  
  221. if (playerExist(uuid)) {
  222.  
  223. ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
  224.  
  225. try {
  226. i = rs.getInt("Kills");
  227. } catch (SQLException e) {
  228. }
  229.  
  230. } else {
  231.  
  232. createPlayer(uuid);
  233. }
  234. }
  235. return i;
  236. }
  237.  
  238. public static int getDeaths(String uuid) {
  239.  
  240. int i = 0;
  241.  
  242. if (isConnected()) {
  243.  
  244. if (playerExist(uuid)) {
  245.  
  246. ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
  247.  
  248. try {
  249. i = rs.getInt("Deaths");
  250. } catch (SQLException e) {
  251. }
  252.  
  253. } else {
  254.  
  255. createPlayer(uuid);
  256. }
  257. }
  258. return i;
  259. }
  260.  
  261. public static void setHighestLevel(String uuid, int Level) {
  262.  
  263. if (isConnected()) {
  264.  
  265. if (playerExist(uuid)) {
  266.  
  267. update("UPDATE GunGame SET Level= '" + Level + "' WHERE UUID= '" + uuid + "';");
  268. } else {
  269.  
  270. createPlayer(uuid);
  271. }
  272. }
  273. }
  274.  
  275. public static int getHighestLevel(String uuid) {
  276.  
  277. int i = 0;
  278.  
  279. if (isConnected()) {
  280.  
  281. if (playerExist(uuid)) {
  282.  
  283. ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
  284.  
  285. try {
  286. i = rs.getInt("Level");
  287. } catch (SQLException e) {
  288. }
  289.  
  290. } else {
  291.  
  292. createPlayer(uuid);
  293. }
  294. }
  295. return i;
  296. }
  297.  
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement