java - H2 找不到适用于 jdbc 的驱动程序 :h2:mem:

标签 java h2

我正在尝试使用 H2 的示例。但我无法创建内存数据库。当我运行以下程序时,我只收到一条错误消息:

java.sql.SQLException: No suitable driver found for jdbc:h2:mem at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251) at db.DBExample.main(DBExample.java:14)

在 pom.xml 中我包含了

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

有人知道哪里出了问题吗?

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DBExample {
    public static void main(String[] a) throws Exception {

        var url = "jdbc:h2:mem:";
        try (var con = DriverManager.getConnection(url);
             var stm = con.createStatement();
             var rs = stm.executeQuery("SELECT 1+1")) {
             if (rs.next()) {
                 System.out.println(rs.getInt(1));
             }
        } catch (SQLException ex) {
            var lgr = Logger.getLogger(DBExample.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
}

最佳答案

问题是因为您指定了 <scope>test</scope> 。 删除范围线。

This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.

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

看看这里:driver not found

关于java - H2 找不到适用于 jdbc 的驱动程序 :h2:mem:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67673707/

相关文章:

java - onCreate 按预期被调用。但表没有创建

java - 是否可以使用 Intent 将数据传递到不同的 Activity ,但不启动该特定 Activity ?

java - 类型不匹配 : cannot convert from org. json.JSONObject 到 org.json.simple.JSONObject

java - GATE Embedded 示例 示例 NoClassFound 错误

java - 如何使用 spark 处理一系列 hbase 行?

java - 恢复 H2 数据库,同时只有 '.db' 扩展文件

java - 使用spring r2dbc时找不到javax.persistence注解

ssl - H2 - 服务器模式下的拆分文件选项

java - H2 中的 SQL 方言

database - H2 和查询日志记录?