1.16.5 H2 Database Driver Error

Discussion in 'Spigot Plugin Development' started by timmy1020, Oct 9, 2021.

  1. Hi, I'm new to H2 and I'm getting a weird error, I've researched it and couldn't find anything here's the stack trace:
    Code (Text):
    [02:40:19 WARN]: There was a problem whilst connecting to the database.
    [02:40:19 WARN]: java.sql.SQLException: No suitable driver found for jdbc:h2:mem:/home/container/plugins/PunishmentGUI/data/PunishmentGUI.db
    [02:40:19 WARN]:        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
    [02:40:19 WARN]:        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
    [02:40:19 WARN]:        at me.tomsoz.punishmentgui.punishmentgui.Database.Database.getConnection(Database.java:16)
    [02:40:19 WARN]:        at me.tomsoz.punishmentgui.punishmentgui.Database.Database.initialiseDatabase(Database.java:25)
    [02:40:19 WARN]:        at me.tomsoz.punishmentgui.punishmentgui.PunishmentGUI.onEnable(PunishmentGUI.java:39)
    [02:40:19 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263)
    [02:40:19 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:370)
    [02:40:19 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:500)
    [02:40:19 WARN]:        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugin(CraftServer.java:518)
    [02:40:19 WARN]:        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugins(CraftServer.java:432)
    [02:40:19 WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.loadWorld(MinecraftServer.java:594)
    [02:40:19 WARN]:        at net.minecraft.server.v1_16_R3.DedicatedServer.init(DedicatedServer.java:298)
    [02:40:19 WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1069)
    [02:40:19 WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:291)
    [02:40:19 WARN]:        at java.base/java.lang.Thread.run(Thread.java:831)
    This is my Database class:
    Code (Java):
    package me.tomsoz.punishmentgui.punishmentgui.Database;

    import me.tomsoz.punishmentgui.punishmentgui.PunishmentGUI;
    import org.bukkit.Bukkit;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.util.logging.Level;

    public class Database {
        public static Connection getConnection() {
            Connection connection = null;
            try {
                connection = DriverManager.getConnection("jdbc:h2:mem:"+ PunishmentGUI.getDbConnectionURL());
            } catch (SQLException e) {
                Bukkit.getLogger().log(Level.WARNING, "There was a problem whilst connecting to the database.");
                e.printStackTrace();
            }
            return connection;
        }

        public static void initialiseDatabase() {
            Connection connection = getConnection();
            PreparedStatement statement;
            try {
                statement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS punishmentgui_logs(id INT NOT NULL PRIMARY KEY IDENTITY(1, 1), status VARCHAR(255) NOT NULL, time BIGINT(20) NOT NULL, target VARCHAR(255) NOT NULL, executor VARCHAR(255) NOT NULL, revoker VARCHAR(255) NULL, punishment_type VARCHAR(255) NOT NULL, duration VARCHAR(255) NOT NULL, reason VARCHAR(255) NOT NULL, revoke_time BIGINT(20) NULL, silent INT NOT NULL");
                statement.execute();
                connection.close();
            } catch(SQLException e) {
                Bukkit.getLogger().log(Level.WARNING, "There was an issue initialising the database.");
                e.printStackTrace();
            }
        }

        public static void addPunishment(String status, long time, String target, String executor, String revoker, String type, String duration, String reason, int revokeTime, int silent) {
            Connection connection = getConnection();
            PreparedStatement statement;
            try {
                statement = connection.prepareStatement("INSERT INTO punishmentgui_logs(status, time, target, executor, revoker, punishment_type, duration, reason, revoke_time, silent) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                statement.setString(1, status);
                statement.setLong(2, time);
                statement.setString(3, target);
                statement.setString(4, executor);
                statement.setString(5, revoker);
                statement.setString(6, type);
                statement.setString(7, duration);
                statement.setString(8, reason);
                statement.setInt(9, revokeTime);
                statement.setInt(10, silent);
                statement.execute();
                connection.close();
            } catch(SQLException e) {
                Bukkit.getLogger().log(Level.WARNING, "There was an issue logging into to the database.");
                e.printStackTrace();
            }
        }
    }
     
    Thanks in advance, Tom
     
  2. There are quite a few posts on StackOverflow that solve your answer, your driver doesn't exist or isn't properly loaded.
     
  3. You have to include driver with your plugin. Shade it.

    For gradle check this.