H2 DB驱动程序不在类路径中 [英] H2 DB driver not in classpath

查看:77
本文介绍了H2 DB驱动程序不在类路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Java连接到我的嵌入式H2数据库.我发现了有关此主题的各种主题和教程,现在有了以下代码:

I'm trying to connect to my embedded H2 database via Java. I found various threads and tutorials on this and now have this code:

Connection con = null;
Properties connectionProps = new Properties();
connectionProps.put("user", "username");
connectionProps.put("password", "password");
try {
    Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

con = DriverManager.getConnection("jdbc:h2:~/test", connectionProps);

我得到了找不到适合jdbc:h2:〜/test的驱动程序"错误信息.我在某些线程中找到Class.forName(...)作为对此的解决方案,但它似乎不起作用(ClassNotFoundException).

I got the "no suitable driver found for jdbc:h2:~/test" error message. I found the Class.forName(...) in some threads as a solution to this, but it doesn't seem to be working (ClassNotFoundException).

我读到该驱动程序可能不在我的类路径中,但是我真的不知道我该如何处理这些信息.在IntelliJ的数据库视图中,驱动程序似乎工作正常(我可以单击重新加载驱动程序",它确认了h2驱动程序).我在做什么错了?

I read that the driver is probably not in my classpath, but don't really know what I need to do with that information. In the database view of IntelliJ the driver seems to work just fine (I can click reload drivers and it confirms the h2 driver). What am I doing wrong?

推荐答案

有几种添加到类路径的方法.我建议尝试理解此页面 https://howtodoinjava.com/java/basics/java-classpath/获取有关具有相似但结果不同的不同策略的更多信息.

There are a couple ways to add to the classpath. I recommend trying to understand this page https://howtodoinjava.com/java/basics/java-classpath/ for more information on different strategies that have similar, but different results.

对于该项目而言,可能最简单的方法是编辑运行配置"(run configuration).您正在使用的计算机,并添加一个新的VM选项:

Probably the simplest way for just this project, would be to edit the "run configuration" that you are using, and add a new VM option:

–classpath /path/to/local/h2/driver.jar

使用一个到jar的绝对路径,这样当以这种方式运行它时,它将允许它出现在类路径中.在我共享的链接中,这与向 java javac 命令添加命令行参数相同,因此此参数将在命令行上的intellij之外工作好吧.

Use an absolute path to the jar, and this will allow it to be present in the classpath when running it this way. In the link I shared, this is the same as adding the command line argument to the java or javac command, so this argument will work outside of intellij on the command line as well.

基于这些注释,您似乎正在使用%PROGRAMFILES%环境变量以及完全限定的路径.

Based on the comments, it seems like you might be using the %PROGRAMFILES% environment variable, with a fully qualified path.

尝试以下方法:

-classpath %PROGRAMFILES%\H2\bin\h2-1.4.200.jar

如果要在命令提示符下运行以下命令

If you were to run the following command in your command prompt

echo %PROGRAMFILES%

我怀疑您会收到答复:

C:\Program Files

不是x86版本.所以.要么使用环境变量,然后忽略其值表示的路径部分(消除 C:\ 部分)

Which is not the x86 version. So. Either use the environment variable and omit the part of the path it's value represents (eliminating the C:\ part)

或者,如果根本不是正确的Program Files文件夹,请避免使用它并尝试(请注意引号,因为文件夹名称中有空格):

or, if that's not the correct Program Files folder at all, then avoid it and try (note the quotes which are required because a folder has spaces in it's name):

--classpath "C:\Program Files (x86)\H2\bin\h2-1.4.200.jar"

这篇关于H2 DB驱动程序不在类路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆