· 6 years ago · Jul 12, 2019, 07:36 PM
1package de.cha0s.system.main;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.ResultSet;
6import java.sql.SQLException;
7import java.sql.Statement;
8
9public class MySQL {
10
11 public MySQL() {
12 // idk
13 }
14
15 public static Connection con;
16
17 public static void connect() {
18 if (!isConnected()) {
19 try {
20 con = DriverManager.getConnection(
21 "jdbc:mysql://" + "localhost" + ":" + 3306
22 + "/" + "X" + "?autoReconnect=true&useUnicode=yes",
23 "X", "X");
24 System.out.println("System >> MySQL Connected");
25 createTable();
26 } catch (SQLException e) {
27 System.err.println("System >> Wrong MySQL Credentials");
28 e.printStackTrace();
29 System.err.println("System >> Wrong MySQL Credentials");
30 }
31 }
32 }
33
34 public static void createTable() {
35 if (isConnected()) {
36 try {
37 con.createStatement().executeUpdate(
38 "CREATE TABLE IF NOT EXISTS registeredPlayers(UUID VARCHAR(36))");
39
40 } catch (SQLException e) {
41
42 }
43 }
44 }
45
46 public void close() {
47 if (isConnected()) {
48 try {
49 con.close();
50 } catch (SQLException e) {
51
52 }
53 }
54 }
55
56 public static boolean isConnected() {
57 return con != null;
58 }
59
60 public void update(String qry) {
61 if (isConnected()) {
62 try {
63 con.createStatement().executeUpdate(qry);
64 } catch (SQLException e) {
65
66 }
67 } else {
68 connect();
69 update(qry);
70 }
71 }
72
73 public static ResultSet getResult(String qry) {
74 if (isConnected()) {
75 ResultSet rs = null;
76 try {
77 Statement st = con.createStatement();
78 rs = st.executeQuery(qry);
79 return rs;
80 } catch (SQLException ex) {
81
82 }
83 return rs;
84 } else {
85 connect();
86 getResult(qry);
87 }
88 return null;
89 }
90
91}