gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:21:43 24 4
gpt4 key购买 nike

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

java.sql.SQLException: No suitable driver found for jdbc:h2:mem atjava.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)atjava.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com