· 7 years ago · Nov 28, 2018, 12:26 AM
1package com.github.Viduality.VSkyblock;
2
3
4import com.github.Viduality.VSkyblock.Utilitys.ConfigShorts;
5
6import java.sql.Connection;
7import java.sql.DriverManager;
8import java.sql.SQLException;
9
10public class MySQLConnector{
11
12 private VSkyblock plugin = VSkyblock.getInstance();
13
14
15
16 /*
17 * returns the username of the mysql user
18 */
19 public String getDbUser() {
20 return plugin.getConfig().getString("database.user");
21 }
22
23
24
25 /*
26 * reutrns the mysql password
27 */
28 public String getDbPasswort() {
29 return plugin.getConfig().getString("database.password");
30 }
31
32
33
34 /*
35 * returns the database name
36 */
37 private String getDatabase() {
38 return plugin.getConfig().getString("database.database");
39 }
40
41
42
43 /*
44 * returns the database url
45 */
46 public String getDbUrl() {
47 return plugin.getConfig().getString("database.url");
48 }
49
50
51
52 /*
53 * returns the database url
54 */
55 public String getDbUrlParameters() {
56 return plugin.getConfig().getString("database.url-parameters");
57 }
58
59
60
61
62 public void initConnection() {
63 ConfigShorts.loaddefConfig();
64 if (getDatabase() != null && !getDatabase().isEmpty()) {
65 initTables();
66 }
67 }
68
69 public Connection getConnection() {
70 try {
71 return DriverManager.getConnection("jdbc:mysql://"
72 + getDbUrl() + "/"
73 + getDatabase(),
74 getDbUser(),
75 getDbPasswort());
76 } catch (SQLException e) {
77 e.printStackTrace();
78 }
79 return null;
80 }
81
82
83 protected void initTables() {
84 Connection connection = getConnection();
85 try {
86 connection.createStatement().execute(
87 "CREATE DATABASE IF NOT EXISTS " + getDatabase());
88 connection.createStatement().execute(
89 "CREATE TABLE IF NOT EXISTS VSkyblock_Player("
90 + "playerid BIGINT AUTO_INCREMENT NOT NULL,"
91 + "playername VARCHAR(50) NOT NULL,"
92 + "uuid VARCHAR(100) UNIQUE NOT NULL,"
93 + "island VARCHAR(100),"
94 + "islandowner BOOLEAN NOT NULL DEFAULT 0,"
95 + "ownerid VARCHAR(100),"
96 + "kicked BOOLEAN NOT NULL DEFAULT 0,"
97 + "PRIMARY KEY (playerid));");
98 connection.createStatement().execute(
99 "CREATE TABLE IF NOT EXISTS VSkyblock_Island("
100 + "islandid BIGING AUTO_INCREMENT NOT NULL,"
101 + "island VARCHAR(100) NOT NULL,"
102 + "islandlevel VARCHAR(100),"
103 + "FOREIGN KEY (island) REFERENCES VSkyblock_Player(island),"
104 + "PRIMARY KEY (islandid))");
105
106 connection.close();
107 } catch (SQLException e) {
108 e.printStackTrace();
109 } finally {
110 closeConnection(connection);
111 }
112 }
113
114
115
116 public void closeConnection(Connection connection) {
117 try {
118 if (connection != null) {
119 connection.close();
120 } else {
121 VSkyblock.getInstance().getLogger().warning("connection = null, VSkyblock can't close");
122 }
123 } catch (SQLException e) {
124 e.printStackTrace();
125 }
126 }
127
128
129 public void close(){
130
131 }
132}