H2/未找到 SQLight 驱动程序

问题描述 投票:0回答:0

我有问题!我想用 H2 做一个项目。我在我的 pom.xml 中添加了依赖项:

 <dependency>
     <groupId>com.h2database</groupId>
     <artifactId>h2</artifactId>
     <version>1.4.200</version>
</dependency>

这是我的连接代码:

public H2(Plugin plugin) {
        this.plugin = plugin;
        this.databaseFile = plugin.getDataFolder().getAbsolutePath() + File.separator + "database";

        connect();
        try {
            initDb();
        } catch (SQLException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void connect() {
        try {
            config.setJdbcUrl("jdbc:h2:" + databaseFile);
            dataSource = new HikariDataSource(config);
            dataSource.getConnection();
            plugin.getLogger().log(Level.INFO, "Successfully connected to H2 database!");
        } catch (SQLException e) {
            plugin.getLogger().log(Level.INFO, "Cannot connect to H2 database! Error: " + e.getMessage());
        }
    }

    public void close() {
        try {
            if (connection != null) {
                connection.close();
                plugin.getLogger().log(Level.INFO, "The H2 database connection has been closed!");
            }
        } catch (SQLException e) {
            plugin.getLogger().log(Level.INFO, "Cannot close H2 database connection! Error: " + e.getMessage());
        }
    }

    private void initDb() throws SQLException, IOException {
        // check if database file exists
        Path path = Paths.get(databaseFile + ".mv.db");
        if (!Files.exists(path)) {
            plugin.getLogger().log(Level.INFO, "Creating new H2 database file: " + databaseFile);
            Files.createFile(path);
        }

        // read database setup file
        String setup;
        try (InputStream in = getClass().getClassLoader().getResourceAsStream("dbsetup.sql")) {
            setup = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
        } catch (IOException e) {
            plugin.getLogger().log(Level.SEVERE, "Could not read H2 database setup file.", e);
            throw e;
        }

        // split setup file into queries
        String[] queries = setup.split(";");

        // execute each query
        for (String query : queries) {
            if (query.isEmpty()) continue;
            try (Connection conn = dataSource.getConnection();
                 PreparedStatement stmt = conn.prepareStatement(query)) {
                stmt.execute();
            }
        }
        plugin.getLogger().info("H2 database setup complete.");
    }

但是如果我加载我的项目,我会得到这个错误:

[23:03:05 WARN]: Exception encountered when loading plugin: EaroBot
java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:h2:/home/proxy/plugins/EaroBot/database
        at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:114) ~[?:?]
        at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:331) ~[?:?]
        at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:114) ~[?:?]
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:108) ~[?:?]
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81) ~[?:?]
        at de.tiiita.earobot.util.database.H2.connect(H2.java:43) ~[?:?]
        at de.tiiita.earobot.util.database.H2.<init>(H2.java:32) ~[?:?]
        at de.tiiita.earobot.EaroBot.onEnable(EaroBot.java:44) ~[?:?]
        at net.md_5.bungee.api.plugin.PluginManager.enablePlugins(Unknown Source) ~[XCord.jar:git:Travertine-Bootstrap:1.19-R0.1-SNAPSHOT:a6b102d:unknown]
        at net.md_5.bungee.BungeeCord.start(Unknown Source) ~[XCord.jar:git:Travertine-Bootstrap:1.19-R0.1-SNAPSHOT:a6b102d:unknown]
        at net.md_5.bungee.BungeeCordLauncher.main(Unknown Source) ~[XCord.jar:git:Travertine-Bootstrap:1.19-R0.1-SNAPSHOT:a6b102d:unknown]
        at net.md_5.bungee.Bootstrap.main(Unknown Source) ~[XCord.jar:git:Travertine-Bootstrap:1.19-R0.1-SNAPSHOT:a6b102d:unknown]
Caused by: java.sql.SQLException: No suitable driver
        at java.sql.DriverManager.getDriver(DriverManager.java:315) ~[?:1.8.0_352]
        at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:106) ~[?:?]
        ... 11 more

我希望插件运行并且我的 h2 类连接到 h2 数据库。我真的不知道为什么会出现此错误。 Java应该找到驱动程序,因为我将它与依赖项一起添加!

java database database-connection h2
© www.soinside.com 2019 - 2024. All rights reserved.