· 9 years ago · Jun 13, 2017, 02:40 PM
1import java.io.File;
2import java.io.IOException;
3import java.io.PrintStream;
4import java.sql.Connection;
5import java.sql.DriverManager;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import java.sql.Statement;
9import org.bukkit.Bukkit;
10import org.bukkit.command.ConsoleCommandSender;
11import org.bukkit.configuration.file.FileConfiguration;
12import org.bukkit.configuration.file.FileConfigurationOptions;
13import org.bukkit.configuration.file.YamlConfiguration;
14
15public class MySQL
16{
17 public static String username;
18 public static String password;
19 public static String database;
20 public static String host;
21 public static String port;
22 public static Connection con;
23
24 public MySQL(String user, String pass, String host2, String dB) {}
25
26 public static void connect()
27 {
28 if (!isConnected()) {
29 try
30 {
31 con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username,
32 password);
33 Bukkit.getConsoleSender().sendMessage("§4MySQL MySQL connected");
34 }
35 catch (SQLException e)
36 {
37 e.printStackTrace();
38 }
39 }
40 }
41
42 public static void close()
43 {
44 if (isConnected()) {
45 try
46 {
47 con.close();
48 Bukkit.getConsoleSender().sendMessage("§4MySQL MySQL disconnected");
49 }
50 catch (SQLException e)
51 {
52 e.printStackTrace();
53 }
54 }
55 }
56
57 public static boolean isConnected()
58 {
59 return con != null;
60 }
61
62 public static void createTable()
63 {
64 if (isConnected()) {
65 try
66 {
67 con.createStatement().executeUpdate(
68 "CREATE TABLE IF NOT EXISTS Stats (UUID VARCHAR(100) , NAME VARCHAR(16), KILLS int, DEATHS int , WIN int , LOSE int , COINS int)");
69 con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Kits (UUID VARCHAR(100), NAME VARCHAR(16), KIT1 int, KIT2 int, KIT3 int, KIT4 int, KIT5 int, KIT6 int, KIT7 int, KIT8 int, KIT9 int, KIT10 int, KIT11 int, KIT12 int, KIT13 int, KIT14 int, KIT15 int, KIT16 int)");
70 Bukkit.getConsoleSender().sendMessage("§4MySQL MySQL Table created");
71 }
72 catch (SQLException e)
73 {
74 e.printStackTrace();
75 }
76 }
77 }
78
79 public static void update(String qry)
80 {
81 if (isConnected()) {
82 try
83 {
84 con.createStatement().executeUpdate(qry);
85 }
86 catch (SQLException e)
87 {
88 e.printStackTrace();
89 }
90 }
91 }
92
93 public static ResultSet getResult(String qry)
94 {
95 ResultSet rs = null;
96 try
97 {
98 Statement st = con.createStatement();
99 rs = st.executeQuery(qry);
100 }
101 catch (SQLException e)
102 {
103 connect();
104 System.err.println(e);
105 }
106 return rs;
107 }
108
109 public static File getMySQLFile()
110 {
111 return new File("plugins/TradeWars", "MySQL.yml");
112 }
113
114 public static FileConfiguration getMySQLFileConfiguration()
115 {
116 return YamlConfiguration.loadConfiguration(getMySQLFile());
117 }
118
119 public static void setStandardMySQL()
120 {
121 FileConfiguration cfg = getMySQLFileConfiguration();
122
123 cfg.options().copyDefaults(true);
124 cfg.addDefault("username", "root");
125 cfg.addDefault("password", "password");
126 cfg.addDefault("database", "localhost");
127 cfg.addDefault("host", "localhost");
128 cfg.addDefault("port", "3306");
129 try
130 {
131 cfg.save(getMySQLFile());
132 }
133 catch (IOException e)
134 {
135 e.printStackTrace();
136 }
137 }
138
139 public static void readMySQL()
140 {
141 FileConfiguration cfg = getMySQLFileConfiguration();
142 username = cfg.getString("username");
143 password = cfg.getString("password");
144 database = cfg.getString("database");
145 host = cfg.getString("host");
146 port = cfg.getString("port");
147 }
148}