· 4 years ago · Jul 16, 2021, 10:34 PM
1public class Database {
2 private MinecraftMentions _plugin;
3 private Connection _connection;
4 public String databaseUrl;
5
6
7 public Database(MinecraftMentions plugin) {
8 this._plugin = plugin;
9 this.databaseUrl = "jdbc:sqlite:" + _plugin.getDataFolder().getAbsolutePath() + "/players.db";
10
11 connect();
12 createNewTable();
13 }
14
15
16 private void connect() {
17 _connection = null;
18 try {
19 // db parameters
20 // create a connection to the database
21 _connection = DriverManager.getConnection(databaseUrl);
22 _connection.setAutoCommit(true);
23 System.out.println("Connection to SQLite has been established.");
24
25 } catch (SQLException e) {
26 System.out.println(e.getMessage());
27 }
28 }
29
30 private void createNewTable() {
31 String sql = "CREATE TABLE IF NOT EXISTS players (\n"
32 + " id integer PRIMARY KEY,\n"
33 + " uuid text NOT NULL,\n"
34 + " enabled boolean NOT NULL\n"
35 + ");";
36
37 Statement stmt = null;
38 try {
39 stmt = _connection.createStatement();
40 stmt.execute(sql);
41 } catch (SQLException throwables) {
42 throwables.printStackTrace();
43 }
44 }
45
46
47 public void checkPlayer(Player player) {
48 String sql = "SELECT EXISTS(SELECT * FROM players WHERE uuid = ?)";
49 PreparedStatement pstmt = null;
50 try {
51 pstmt = _connection.prepareStatement(sql);
52 pstmt.setString(1, player.getUniqueId().toString());
53 ResultSet rs = pstmt.executeQuery();
54
55 if (!rs.next()) {
56 String insert_sql = "INSERT INTO players(uuid, enabled) VALUES(?, ?)";
57 PreparedStatement insert_pstmt = null;
58
59 insert_pstmt = _connection.prepareStatement(insert_sql);
60 insert_pstmt.setString(1, player.getUniqueId().toString());
61 insert_pstmt.setBoolean(2, true);
62
63 insert_pstmt.executeUpdate();
64 }
65 } catch (SQLException throwables) {
66 throwables.printStackTrace();
67 }
68 }
69
70 public boolean getPlayerEnabled(Player player) {
71 checkPlayer(player);
72
73 String sql = "SELECT enabled FROM players WHERE uuid = ?";
74 PreparedStatement pstmt = null;
75
76 try {
77 pstmt = _connection.prepareStatement(sql);
78 pstmt.setString(1, player.getUniqueId().toString());
79
80 ResultSet rs = pstmt.executeQuery();
81 if (rs.next()) {
82 return rs.getBoolean("enabled");
83 }
84 } catch (SQLException throwables) {
85 throwables.printStackTrace();
86 }
87
88 return true;
89 }
90
91 public void updatePlayerEnabled(Player player, Boolean enabled) {
92 String sql = "UPDATE players SET enabled = ? WHERE uuid = ?";
93 PreparedStatement pstmt = null;
94 System.out.println("Updating player");
95 try {
96 pstmt = _connection.prepareStatement(sql);
97 pstmt.setBoolean(1, enabled);
98 pstmt.setString(2, player.getUniqueId().toString());
99
100 pstmt.executeUpdate();
101 } catch (SQLException throwables) {
102 throwables.printStackTrace();
103 }
104 }
105
106 public void disconnect() {
107 if (_connection != null) {
108 try {
109 _connection.close();
110 } catch (SQLException throwables) {
111 throwables.printStackTrace();
112 }
113 }
114 }
115
116}