HikariPool: Column 'LOCALTIME' not found

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!

Hi. Your pastebin url is the default url so clicking it goes to the paste your log page.

1 Like

Oh my bad! Can’t believe I didnt notice that!

I believe you want to use current_timestamp() instead of LOCALTIME… LOCALTIME does not exist within the spec as far as I can tell.

It does, but I think you’re right, there is a difference between TIME and TIMESTAMP data types - meaning that current_timestamp() or LOCALTIMESTAMP would be correct, depending on the scenario.

Mind you, I wonder if these LOCAL* functions only exist in newer versions of H2 that we aren’t shipping.

I couldn’t get LOCALTIME even with latest in DataGrip, so I assume it may have been a special web function or something?

Thanks for the suggestions!

I gave LOCALTIMESTAMP a try and received an identical error. (Obviously mentioning the new function instead).

current_timestamp() works and now I am able to extract data from my database!

As a side note, does anyone know if I can create a H2 database without a username and password? This H2 software does not let you create a database without login details.