@@ -26,6 +26,7 @@ public final class Database {
2626 """ ;
2727 private static final String SELECT_BY_PLAYER = "SELECT `server` FROM last_server WHERE `player` = ?;" ;
2828 private static final String INSERT_DATA = "INSERT INTO last_server(`player`, `server`) VALUES (?, ?);" ;
29+ private static final String UPDATE_SERVER = "UPDATE last_server SET `server` = ? WHERE `player` = ?;" ;
2930
3031 private final HikariDataSource source ;
3132 private final Logger logger ;
@@ -37,13 +38,12 @@ public Database(
3738 if (Files .notExists (dataDirectory )) {
3839 Files .createDirectory (dataDirectory );
3940 }
40- final Path databasePath = dataDirectory .resolve ("database.db" ).toAbsolutePath ();
41- if (Files .notExists (databasePath )) {
42- Files .createFile (databasePath );
43- }
41+ final Path databasePath = dataDirectory .resolve ("database" ).toAbsolutePath ();
4442 final HikariConfig hikariConfig = new HikariConfig ();
4543 hikariConfig .setDriverClassName ("org.h2.Driver" );
4644 hikariConfig .setJdbcUrl ("jdbc:h2:" +databasePath );
45+ hikariConfig .setUsername ("sa" );
46+ hikariConfig .setPassword ("" );
4747
4848 this .source = new HikariDataSource (hikariConfig );
4949 this .logger = logger ;
@@ -68,7 +68,14 @@ public void setLastServer(final String playerName, final String server) {
6868 statement .setString (2 , server );
6969 statement .executeUpdate ();
7070 } catch (final SQLException e ) {
71- this .logger .warn ("An error occurred updating last server information of player {}" , playerName );
71+ try (final Connection connection = this .source .getConnection ();
72+ final PreparedStatement statement = connection .prepareStatement (UPDATE_SERVER );
73+ ) {
74+ statement .setString (1 , server );
75+ statement .setString (2 , playerName );
76+ } catch (SQLException ex ) {
77+ this .logger .warn ("An error occurred updating last server information of player {}" , playerName , ex );
78+ }
7279 }
7380 }
7481
0 commit comments