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