· 9 years ago · Aug 07, 2017, 10:56 PM
1 private static MySQL instance = new MySQL();
2 public Connection connection;
3 public String host;
4 public String database;
5 public String user;
6 public String password;
7 public int port;
8 public String table;
9 public String uuid;
10 public String toggleChat;
11
12 public static MySQL getInstance() {
13 return instance;
14 }
15
16
17 public void mysqlSetUp(){
18 host = SpookyProfiles.pl.getConfig().getString("host");
19 port = SpookyProfiles.pl.getConfig().getInt("port");
20 database = SpookyProfiles.pl.getConfig().getString("database");
21 user = SpookyProfiles.pl.getConfig().getString("user");
22 password = SpookyProfiles.pl.getConfig().getString("password");
23 table = SpookyProfiles.pl.getConfig().getString("table");
24 uuid = SpookyProfiles.pl.getConfig().getString("UUID");
25 toggleChat = SpookyProfiles.pl.getConfig().getString("togglechat");
26
27 try {
28 synchronized (this) {
29 if (getConnection() != null && !getConnection().isClosed()) {
30 return;
31 }
32
33 Class.forName("com.mysql.jdbc.Driver");
34 setConnection(DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" +
35 this.database, this.user, this.password));
36 Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL Connected!");
37
38 }
39 } catch (SQLException e) {
40 e.printStackTrace();
41 } catch (ClassNotFoundException e) {
42 e.printStackTrace();
43 }
44 }
45
46
47 public Connection getConnection() {
48 return connection;
49 }
50
51 public void setConnection(Connection connection) {
52 this.connection = connection;
53 }
54
55 public void createTable(){
56 String create = "CREATE TABLE IF NOT EXISTS " + table + "(" + uuid + " VARCHAR(36), " + toggleChat +
57 " INT NOT NULL, PRIMARY KEY(" + uuid + "))";
58 }
59
60 public void disconnect() {
61 if (getConnection() == null) {
62 return;
63 }
64 try {
65 getConnection().close();
66 Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "MySQL has disconnected!");
67 } catch (SQLException e) {
68 e.printStackTrace();
69 }
70 }
71}