· 9 years ago · Jul 02, 2017, 12:08 AM
1private String reason;
2 public Connection con;
3 public Statement statement;
4 private static boolean isOffline;
5
6 /**
7 * Creates a new connection to the database and connects automatically
8 *
9 * @param reason The reason for the connection
10 */
11 public MySQLUtil(String reason) {
12 this.reason = reason;
13 connect();
14 }
15
16 /**
17 * Connects to the database. Note: The connection is already established when you create a new object
18 */
19 public void connect() {
20 try {
21 con = DriverManager.getConnection("jdbc:mysql://66.160.237.2:3306/SpookyMC", "root", "s8No4p8rS");
22 System.out.println("Created Connection [" + reason.toUpperCase() + "]");
23 this.statement = con.createStatement();
24 } catch (SQLException e) {
25 e.printStackTrace();
26 System.out.println("Failed To Create Connection. [" + reason.toUpperCase() + "]");
27 System.out.println("Unable to connect. Please check to ensure" +
28 " connection is working, and MySQL information is correct.");
29 System.out.println("No longer trying to do anything database related to prevent further errors. "
30 + "Restart the server when you believe that the problem has been fixed.");
31 isOffline = true;
32 }
33 }
34
35 /**
36 * Closes the connection
37 */
38 public void disconnect() {
39 try {
40 con.close();
41 System.out.println("Closed Connection [" + reason.toUpperCase() + "]");
42 } catch (SQLException e) {
43 e.printStackTrace();
44 }
45 }
46
47 /**
48 * @see #disconnect()
49 */
50 public void close() {
51 disconnect();
52 }
53
54 /**
55 * @return The reason that was given when the object was created
56 */
57 public String getReason() {
58 return reason;
59 }
60
61 public ResultSet executeQuery(String que) {
62 try {
63 return statement.executeQuery(que);
64 } catch (SQLException e) {
65 System.out.println("Failed to execute query. [" + reason.toUpperCase() + "] [" + que + "]");
66 e.printStackTrace();
67 }
68 return null;
69 }
70
71 public void executeCommand(String command) {
72 try {
73 PreparedStatement statement = con.prepareStatement(command);
74 statement.executeUpdate();
75 statement.close();
76 } catch (SQLException e) {
77 System.out.println("Failed to execute update. [" + reason.toUpperCase() + "] [" + command + "]");
78 e.printStackTrace();
79 }
80 }
81
82 /**
83 * Creates the table 'Build' in the database if it does not exist yet
84 */
85 public static void createTable() {
86 new Thread(new BukkitRunnable() {
87 public void run() {
88 if (isOffline == false) {
89 return;
90 }
91 MySQLUtil dc = new MySQLUtil("check_table_exists");
92 Connection con = dc.con;
93
94 String que =
95 "CREATE TABLE IF NOT EXISTS SPOOKYPROFILES (UUID varchar(36) NOT NULL, chatOn varchar(20), PRIMARY KEY (UUID))";
96 try {
97 java.sql.Statement state = con.createStatement();
98 state.execute(que);
99 state.close();
100 dc.close();
101 } catch (SQLException e) {
102 e.printStackTrace();
103 dc.close();
104 }
105 }
106 }).start();
107 }
108
109 public static boolean checkStillOffline() {
110 try {
111 Connection con = DriverManager.getConnection("jdbc:mysql://66.160.237.2:3306/SpookyMC", "root", "s8No4p8rS");
112 System.out.println("Created Connection [CHECK_OFFLINE]");
113 isOffline = false;
114 return false;
115 } catch (SQLException e) {
116 e.printStackTrace();
117 System.out.println("Failed To Create Connection. [CHECK_OFFLINE]");
118 System.out.println("Unable to connect. Please check to ensure" +
119 " connection is working, and MySQL information is correct.");
120 System.out.println("No longer trying to do anything database related to prevent further errors. "
121 + "Restart the server when you believe that the problem has been fixed.");
122 isOffline = true;
123 return true;
124 }
125 }
126}