jok*_*ker 13 java jdbc classpath h2 maven
我遇到了关于连接到H2的问题
这是我的pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>.</groupId>
<artifactId>dbConnection</artifactId>
<name>Db Connection</name>
<packaging>war</packaging>
<version>0.1</version>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.176</version>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
这是我的主要代码
import java.sql.*;
public class DbConnection
{
static final String DB_URL = "jdbc:h2:tcp://localhost/~/test;AUTO_SERVER=TRUE";
public static void main(String[] args) throws Exception
{
try
{
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(DB_URL,"sa","");
conn.close();
}
catch(ClassNotFoundException ex)
{
System.out.println( "ERROR: Class not found: " + ex.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
总是显示未找到的类:org.h2.Driver
小智 28
您应该将范围设置为运行时,以便将h2驱动程序打包在war文件中:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
<scope>runtime</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我遇到了与IntelliJ相同的问题,但是找不到它org.h2.Driver.我尝试了几种来自Web的解决方案,但在简单重启IntelliJ之后问题就解决了.
希望这有助于节省一些时间.
在这里找到答案删除运行时范围
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
#removed this -> <scope>test</scope> #
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29087 次 |
| 最近记录: |