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
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.
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:
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 .
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With