-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSql.java
More file actions
79 lines (66 loc) · 2.16 KB
/
Sql.java
File metadata and controls
79 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package fr.revivemc.data;
import fr.revivemc.Main;
import fr.revivemc.data.wrapper.Players;
import org.bukkit.entity.Player;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.UUID;
public class Sql {
// Information of connection
private Connection connection;
/**
* This function allows you to create the different tables
* @param stmt A parameter to run SQL commands
* @throws SQLException Returns an exception if a sql error occurred
*/
private void createTable(Statement stmt) throws SQLException {
stmt.execute("CREATE TABLE IF NOT EXISTS players (uuid TEXT PRIMARY KEY, kill INTEGER, death INTEGER)");
}
/**
* Allows you to close the db
*/
public void close() {
try {
if (connection != null && !connection.isClosed())
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Allows you to initialize the db when the server is launched
*/
public void init() {
try {
Class.forName("org.sqlite.JDBC");
File data_folder = Main.getPlugin(Main.class).getDataFolder();
connection = DriverManager.getConnection("jdbc:sqlite:" + data_folder + "/data.db");
Statement statement = connection.createStatement();
createTable(statement);
statement.close();
System.out.println("Database connection established");
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* Save data in the db
*/
public void saveData() {
for (Map.Entry<UUID, Cache> cache : Main.cache.entrySet()) {
Players.addPlayer(cache.getKey());
Players.setKillByUuid(cache.getKey(), cache.getValue().getKills());
Players.setDeathByUuid(cache.getKey(), cache.getValue().getDeaths());
}
}
/**
* @return Returns the connection
*/
public Connection getConnection() {
return (connection);
}
}