· 9 years ago · Feb 08, 2017, 02:02 PM
1package mysql;
2
3import java.io.File;
4import java.io.IOException;
5import java.sql.Connection;
6import java.sql.DriverManager;
7import java.sql.ResultSet;
8import java.sql.SQLException;
9import java.sql.Statement;
10
11import org.bukkit.Bukkit;
12import org.bukkit.configuration.file.FileConfiguration;
13import org.bukkit.configuration.file.YamlConfiguration;
14
15public class MySQL {
16 public static String username;
17 public static String password;
18 public static String database;
19 public static String host;
20 public static String port;
21 public static Connection con;
22
23 public MySQL(String user, String pass, String host2, String dB) {
24
25 }
26
27 public static void connect() {
28 if (!isConnected()) {
29 try {
30 con = DriverManager.getConnection(
31 "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username,
32 password);
33 Bukkit.getConsoleSender().sendMessage("MySQL MySQL connected");
34 } catch (SQLException e) {
35 e.printStackTrace();
36 }
37 }
38 }
39
40 public static void close() {
41 if (isConnected()) {
42 try {
43 con.close();
44 Bukkit.getConsoleSender().sendMessage("MySQL MySQL disconnected");
45 } catch (SQLException e) {
46 e.printStackTrace();
47 }
48 }
49 }
50
51 public static boolean isConnected() {
52 return con != null;
53 }
54
55 public static void createTable() {
56 if (isConnected()) {
57 try {
58 con.prepareStatement( "CREATE TABLE IF NOT EXISTS `SkyPvP` (`UUID` VARCHAR(100), `NAME` VARCHAR(80), `KILLS` INT(30), `DEATHS` INT(30), `POINTS` INT(30))" ).execute();
59
60 Bukkit.getConsoleSender().sendMessage("MySQL MySQL Table created");
61 } catch (SQLException e) {
62 e.printStackTrace();
63 }
64 }
65 }
66
67 public static void update(String qry) {
68 if (isConnected()) {
69 try {
70 con.createStatement().executeUpdate(qry);
71 } catch (SQLException e) {
72 e.printStackTrace();
73 }
74 }
75 }
76
77 public static ResultSet getResult(String qry) {
78 ResultSet rs = null;
79 try {
80 Statement st = con.createStatement();
81 rs = st.executeQuery(qry);
82 } catch (SQLException e) {
83 connect();
84 System.err.println(e);
85 }
86 return rs;
87 }
88
89 public static File getMySQLFile() {
90 return new File("plugins/Avenoox", "MySQL.yml");
91 }
92
93 public static FileConfiguration getMySQLFileConfiguration() {
94 return YamlConfiguration.loadConfiguration(getMySQLFile());
95 }
96
97 public static void setStandardMySQL() {
98 FileConfiguration cfg = getMySQLFileConfiguration();
99
100 cfg.options().copyDefaults(true);
101 cfg.addDefault("username", "root");
102 cfg.addDefault("password", "password");
103 cfg.addDefault("database", "localhost");
104 cfg.addDefault("host", "localhost");
105 cfg.addDefault("port", "3306");
106 try {
107 cfg.save(getMySQLFile());
108 } catch (IOException e) {
109 e.printStackTrace();
110 }
111 }
112
113 public static void readMySQL() {
114 FileConfiguration cfg = getMySQLFileConfiguration();
115 username = cfg.getString("username");
116 password = cfg.getString("password");
117 database = cfg.getString("database");
118 host = cfg.getString("host");
119 port = cfg.getString("port");
120 }
121}