Hey everyone!
I come with a database problem. I have a H2 database which I created using the following statement. (This is done before launching anything using the H2 Console).
CREATE TABLE Players(
uuid CHAR(36) PRIMARY KEY NOT NULL,
mining INT NOT NULL DEFAULT 1,
mining_xp INT NOT NULL DEFAULT 0,
mining_active_right_click TIMESTAMP NOT NULL DEFAULT LOCALTIME,
woodcutting INT NOT NULL DEFAULT 1,
woodcutting_xp INT NOT NULL DEFAULT 0,
woodcutting_active_right_click TIMESTAMP NOT NULL DEFAULT LOCALTIME,
excavation INT NOT NULL DEFAULT 1,
excavation_xp INT NOT NULL DEFAULT 0,
excavation_active_right_click TIMESTAMP NOT NULL DEFAULT LOCALTIME,
herbalism INT NOT NULL DEFAULT 1,
herbalism_xp INT NOT NULL DEFAULT 0,
fishing INT NOT NULL DEFAULT 1,
fishing_xp INT NOT NULL DEFAULT 0
);
After creating the table in the H2 web interface, I add myself as a row using the following statement:
INSERT INTO Players(uuid) VALUES ('4fcc17da-7951-44fa-beb0-813376c149a0');
Which works perfectly giving the following result: https://i.imgur.com/YiT6m0V.png
Now, in my plugin, I use the following class to get the mining level of myself so that I can test my code:
package io.github.carlhalstead.minerpg;
import com.google.inject.Singleton;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.service.sql.SqlService;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@Singleton
public class Database {
private SqlService sqlService;
private final String dbUrl = "jdbc:h2:./config/minerpg/minerpg.db";
private DataSource getDatabase(String url) throws SQLException {
if(sqlService == null)
sqlService = Sponge.getServiceManager().provide(SqlService.class).get();
return sqlService.getDataSource(url);
}
public int getMiningLevel(String playerUUID) throws SQLException {
String query = "SELECT mining FROM players WHERE uuid = '4fcc17da-7951-44fa-beb0-813376c149a0' LIMIT 1";
try(Connection connection = getDatabase(dbUrl).getConnection();
PreparedStatement statement = connection.prepareStatement(query);
ResultSet results = statement.executeQuery()){
while(results.next()){
return results.getInt(1);
}
throw new SQLException();
}
}
}
This gets called in the onPlayerJoined event:
@Listener
public void onPlayerJoined(ClientConnectionEvent.Login event){
try{
final int carlMiningLevel = database.getMiningLevel(event.getTargetUser().getUniqueId().toString());
logger.info("Carls mining level is: " + carlMiningLevel);
} catch (SQLException e) {
e.printStackTrace();
}
}
However I get the following exception stacktrace: https://pastebin.com/
I do not even know where to start with fixing this.
Thanks!