· 8 years ago · Jun 14, 2018, 09:40 PM
1package me.tixmcffa.Util;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import org.bukkit.Bukkit;
9
10
11public class MySQL
12{
13 public static String Host = FileManager.MySQLCfg.getString("MySQL.Host");
14 public static int Port = FileManager.MySQLCfg.getInt("MySQL.Port");
15 public static String User = FileManager.MySQLCfg.getString("MySQL.User");
16 public static String Password = FileManager.MySQLCfg.getString("MySQL.Password");
17 public static String DataBase = FileManager.MySQLCfg.getString("MySQL.DataBase");
18 public static Connection Con;
19
20 public static boolean isConnected()
21 {
22 return Con != null;
23 }
24
25 public static void Connect()
26 {
27 if (!isConnected()) {
28 try
29 {
30 Con = DriverManager.getConnection("jdbc:mysql://" + Host + ":" + 3306 + "/" + DataBase, User, Password);
31 Bukkit.getConsoleSender().sendMessage("§aMySQL connection established successfully.");
32 }
33 catch (SQLException e)
34 {
35 Bukkit.getConsoleSender().sendMessage("§cFailed to connect to the MySQL database.");
36 }
37 }
38 }
39
40 public static void close()
41 {
42 if (isConnected()) {
43 try
44 {
45 Con.close();
46 Bukkit.getConsoleSender().sendMessage(
47 "§2MySQL connection closed succesfully!");
48 }
49 catch (SQLException e)
50 {
51 e.printStackTrace();
52 }
53 }
54 }
55
56 public static void update(String qry)
57 {
58 if (isConnected()) {
59 try
60 {
61 PreparedStatement pt = Con.prepareStatement(qry);
62 pt.executeUpdate();
63 }
64 catch (SQLException e)
65 {
66 e.printStackTrace();
67 }
68 }
69 }
70
71 public static ResultSet getResult(String qry)
72 {
73 if (isConnected()) {
74 try
75 {
76 PreparedStatement pt = Con.prepareStatement(qry);
77
78 return pt.executeQuery();
79 }
80 catch (SQLException e)
81 {
82 e.printStackTrace();
83 return null;
84 }
85 }
86 return null;
87 }
88
89 public static void createTables()
90 {
91 if (isConnected()) {
92 try
93 {
94 Con.createStatement().executeUpdate(
95 "CREATE TABLE IF NOT EXISTS FFA (Playername VARCHAR(100), UUID VARCHAR(100),Rank int, Points int, Kills int, Deaths int)");
96 }
97 catch (SQLException e)
98 {
99 e.printStackTrace();
100 }
101 }
102 }
103}