为什么我无法获得org.h2.Driver?我用Maven [英] Why I can't get the org.h2.Driver? I use maven

查看:1889
本文介绍了为什么我无法获得org.h2.Driver?我用Maven的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到有关连接H2的问题

I face a problem about connecting to H2

这是我的pom.xml:

this is my 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>

这是我的主要代码

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()); 
           }
    }
}

总是显示未找到类:org.h2.Driver

is always show up that Class not found:org.h2.Driver

推荐答案

您应该将范围设置为运行时,以便将h2驱动程序打包在war文件中:

You should set scope to runtime so that h2 driver is packaged in your war file:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.190</version>
    <scope>runtime</scope>
</dependency>

这篇关于为什么我无法获得org.h2.Driver?我用Maven的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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