19
19
20
20
public class SQLHandler {
21
21
22
- Connection connection ;
22
+ private Connection connection ;
23
23
Logger logger = SimpleHomes .getInstance ().getLogger ();
24
24
25
25
private SQLHandler () {
@@ -35,23 +35,22 @@ public static SQLHandler getInstance() {
35
35
}
36
36
37
37
public void init () {
38
- try {
39
- connection = sqlOrSqlLite ();
40
- try (Statement statement = connection .createStatement ()) {
41
- statement .execute ("""
42
- CREATE TABLE IF NOT EXISTS homes (
43
- player_uuid_and_name VARCHAR (255) PRIMARY KEY,
44
- player_uuid VARCHAR(36),
45
- home_name VARCHAR(255),
46
- world_uuid VARCHAR(255),
47
- world_name VARCHAR(255),
48
- location_x DOUBLE,
49
- location_y DOUBLE,
50
- location_z DOUBLE,
51
- yaw FLOAT,
52
- pitch FLOAT
53
- );""" );
54
- }
38
+
39
+ try (Statement statement = getConnection ().createStatement ()) {
40
+ statement .execute ("""
41
+ CREATE TABLE IF NOT EXISTS homes (
42
+ player_uuid_and_name VARCHAR (255) PRIMARY KEY,
43
+ player_uuid VARCHAR(36),
44
+ home_name VARCHAR(255),
45
+ world_uuid VARCHAR(255),
46
+ world_name VARCHAR(255),
47
+ location_x DOUBLE,
48
+ location_y DOUBLE,
49
+ location_z DOUBLE,
50
+ yaw FLOAT,
51
+ pitch FLOAT
52
+ );""" );
53
+
55
54
} catch (SQLException e ) {
56
55
logger .severe ("Failed to connect to SQLite database" );
57
56
logger .severe ("Error: " + e .getMessage ());
@@ -65,7 +64,7 @@ public List<Home> getHomes(UUID uuid) {
65
64
}
66
65
List <Home > homes = new ArrayList <>();
67
66
String query = "SELECT * FROM homes WHERE player_uuid = ?" ;
68
- try (PreparedStatement statement = connection .prepareStatement (query )) {
67
+ try (PreparedStatement statement = getConnection () .prepareStatement (query )) {
69
68
statement .setString (1 , uuid .toString ());
70
69
try (ResultSet resultSet = statement .executeQuery ()) {
71
70
while (resultSet .next ()) {
@@ -101,13 +100,13 @@ public List<Home> getHomes(UUID uuid) {
101
100
public boolean deleteHome (UUID uuid , String homeName ) {
102
101
// Prepare the SQL statement to check if the home exists
103
102
String checkIfExistsQuery = "SELECT * FROM homes WHERE player_uuid = ? AND home_name = ?" ;
104
- try (PreparedStatement homeExists = connection .prepareStatement (checkIfExistsQuery )) {
103
+ try (PreparedStatement homeExists = getConnection () .prepareStatement (checkIfExistsQuery )) {
105
104
homeExists .setString (1 , uuid .toString ());
106
105
homeExists .setString (2 , homeName );
107
106
try (ResultSet resultSet = homeExists .executeQuery ()) {
108
107
if (resultSet .next ()) { // Home exists
109
108
String deleteQuery = "DELETE FROM homes WHERE player_uuid = ? AND home_name = ?" ;
110
- try (PreparedStatement deleteStatement = connection .prepareStatement (deleteQuery )) {
109
+ try (PreparedStatement deleteStatement = getConnection () .prepareStatement (deleteQuery )) {
111
110
deleteStatement .setString (1 , uuid .toString ());
112
111
deleteStatement .setString (2 , homeName );
113
112
deleteStatement .executeUpdate ();
@@ -127,9 +126,9 @@ public boolean deleteHome(UUID uuid, String homeName) {
127
126
128
127
public boolean setHome (UUID uuid , Location location , String homeName ) {
129
128
String insertQuery = "REPLACE INTO homes " +
130
- "(player_uuid_and_name, player_uuid, home_name, world_uuid, location_x, location_y, location_z, yaw, pitch) " +
131
- "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" ;
132
- try (PreparedStatement insertStatement = connection .prepareStatement (insertQuery )) {
129
+ "(player_uuid_and_name, player_uuid, home_name, world_uuid, location_x, location_y, location_z, yaw, pitch) " +
130
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" ;
131
+ try (PreparedStatement insertStatement = getConnection () .prepareStatement (insertQuery )) {
133
132
insertStatement .setString (1 , uuid + homeName );
134
133
insertStatement .setString (2 , uuid .toString ());
135
134
insertStatement .setString (3 , homeName );
@@ -153,7 +152,7 @@ public boolean setHome(UUID uuid, Location location, String homeName) {
153
152
private void updateCache (UUID uuid ) {
154
153
List <Home > homes = new ArrayList <>();
155
154
String query = "SELECT * FROM homes WHERE player_uuid = ?" ;
156
- try (PreparedStatement statement = connection .prepareStatement (query )) {
155
+ try (PreparedStatement statement = getConnection () .prepareStatement (query )) {
157
156
statement .setString (1 , uuid .toString ());
158
157
try (ResultSet resultSet = statement .executeQuery ()) {
159
158
while (resultSet .next ()) {
@@ -183,6 +182,13 @@ public void removePlayerFromCache(UUID uuid) {
183
182
cachedHomes .remove (uuid );
184
183
}
185
184
185
+ private Connection getConnection () throws SQLException {
186
+ if (connection == null || connection .isClosed () || !connection .isValid (2 )) {
187
+ connection = sqlOrSqlLite ();
188
+ }
189
+ return connection ;
190
+ }
191
+
186
192
private Connection sqlOrSqlLite () throws SQLException {
187
193
if (ConfigHandler .getInstance ().isUsingMysql ()) {
188
194
return DriverManager .getConnection ("jdbc:mysql://" + ConfigHandler .getInstance ().getIp () + "/" + ConfigHandler .getInstance ().getName (), ConfigHandler .getInstance ().getUsername (), ConfigHandler .getInstance ().getPassword ());
0 commit comments