· 6 years ago · Jan 11, 2020, 04:42 PM
1package com.angelsmc.database;
2
3import java.sql.DriverManager;
4import java.sql.PreparedStatement;
5import java.sql.SQLException;
6
7import org.bukkit.Bukkit;
8
9import com.mysql.jdbc.Connection;
10
11public class DBMain {
12
13 private String host, username, password, database;
14 private Connection connection;
15 public DBMain(String host, String username, String password, String database) {
16 this.host = host;
17 this.username = username;
18 this.password = password;
19 this.database = database;
20 }
21 public String getHost() {
22 return host;
23 }
24 public String getUsername() {
25 return username;
26 }
27 public String getDatabase() {
28 return database;
29 }
30 public String getPassword() {
31 return password;
32 }
33 public int getPort() {
34 return 3306;
35 }
36 public boolean isConnected() {
37 try {
38 return !connection.isClosed();
39 } catch (SQLException e) {
40 // TODO Auto-generated catch block
41 e.printStackTrace();
42 }
43 return false;
44 }
45 public Connection getConnection() {
46 return connection;
47 }
48 public void connect() throws SQLException, ClassNotFoundException {
49 if(connection != null && !connection.isClosed()) {
50 return;
51 }
52 synchronized (this) {
53 if(connection != null && !connection.isClosed()) {
54 return;
55 }
56 Class.forName("com.mysql.jdbc.Driver");
57 connection = (Connection) DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + 3306 + "/" + this.database+"?autoReconnect=true", this.username, this.password);
58 }
59 }
60 public void createTables() throws SQLException {
61 String query =
62"CREATE TABLE IF NOT EXISTS AngelsSpy(UUID varchar(37), username varchar(37));";
63 PreparedStatement stmt = this.getConnection().prepareStatement(query);
64 stmt.executeUpdate();
65 Bukkit.getConsoleSender().sendMessage("§cTable has been successfully loaded.");
66 }
67
68}