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