Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't get the org.h2.Driver? I use maven

I face a problem about connecting to H2

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>

and this is my main code

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

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

like image 285
joker Avatar asked Sep 02 '15 08:09

joker


People also ask

Where can I find the source code for the H2 platform?

To take advantage of H2 specific features, use the H2Platform . The source code of this platform is included in H2 at src/tools/oracle/toplink/essentials/platform/database/DatabasePlatform.java.txt . You will need to copy this file to your application, and rename it to .java.

What do I need to run the H2 console?

This can be a H2 database, or another database that supports the JDBC API. This is a client/server application, so both a server and a client (a browser) are required to run it. Depending on your platform and environment, there are multiple ways to start the H2 Console:

How do I use H2 in EclipseLink?

To use H2 in EclipseLink, use the platform class org.eclipse.persistence.platform.database.H2Platform . If this platform is not available in your version of EclipseLink, you can use the OraclePlatform instead in many case. See also H2Platform .

How to get WebDriver using Maven?

We can get WebDriver using Maven and let's try to sort out everything step by step. Right-click on your package Explorer and create a new project. Search for maven and create a Maven project. The Group Id is nothing but a unique identifier that owns the project.


2 Answers

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>
like image 185
Marko Jurisic Avatar answered Sep 20 '22 15:09

Marko Jurisic


I had the same problem with IntelliJ, it could not found org.h2.Driver. I tried several solutions from web but after simple restart of IntelliJ the problem was solved.

Hope this helps to save some time.

like image 27
Vitalii Avatar answered Sep 21 '22 15:09

Vitalii