hamnghi
hamnghi

Reputation: 99

Class Not Found Exception org//h2//Driver

I am learning Spring Framework and trying to inject properties from the .properties file.

This is my .properties file

sprint.datasource.username=hamnghi
sprint.datasource.password=hamnghi
sprint.datasource.url=jdbc:h2:~/test;
sprint.datasource.driver=org.h2.Driver;

When I tried to pass the driver field into the Class.forName(drive), the program could not connect to the database and threw a java.lang.ClassNotFoundException: org/h2/Driver; but it printed the driver variable as "org.h2.Driver" to the console just fine. My console screenshot

I also ran the program with Class.forName("org.h2.Driver"), and it ran fine; however, when I replaced it with the driver, it didn't work

This is my class.

package H2Database.db_connection;

import H2Database.functionality.Logging;
import org.springframework.beans.factory.annotation.Value;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

public class H2Connection {
    private final static Logger logger = Logging.getLogger();

    @Value("${sprint.datasource.url}")
    private String url;

    @Value("${sprint.datasource.username}")
    private String username;

    @Value("${sprint.datasource.password}")
    private String password;

    @Value("${sprint.datasource.driver}")
    private  String driver;

    public Connection open(){
        try {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, username, password);
            return connection;
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String toString() {
        return "H2Connection{" +
                "url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", driver='" + driver + '\'' +
                '}';
    }
}

Upvotes: 0

Views: 4093

Answers (3)

Sandeep Dixit
Sandeep Dixit

Reputation: 1036

I had included following in pom.xml

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

And also had these properties in the application.properties file.

spring.datasource.url=jdbc:h2:mem:testdb  
spring.datasource.driverClassName=org.h2.Driver  
spring.datasource.username=sa  
spring.datasource.password=  
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

I was still getting the error.

This was because I was not running project via mvn command, which ment that mvn did not download newly added dependencies.

Then I reloaded the project using Project context menu> Maven > Reload Project.

Depedency was downloaded then it worked.

Upvotes: 0

Hasitha Amarathunga
Hasitha Amarathunga

Reputation: 2005

You should complete the following steps to Configure the H2 database to your Spring MVC application.

Step 1: Add the following dependency in the pom.xml file

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

Step 2: configure these properties in the application.properties file.

spring.datasource.url=jdbc:h2:mem:testdb  
spring.datasource.driverClassName=org.h2.Driver  
spring.datasource.username=sa  
spring.datasource.password=  
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Note: Spring MVC does not auto configure the properties of the application.properties. You should configure it using configuration class.

Step 3: Configure the Database.

@Configuration
@PropertySource("classpath:application.properties")
public class DataSourceConfig {

    @Value("${sprint.datasource.url}")
    private String url;

    @Value("${sprint.datasource.username}")
    private String username;

    @Value("${sprint.datasource.password}")
    private String password;

    @Value("${sprint.datasource.driver}")
    private  String driver;

    @Bean
    public DataSource testDataSource() {
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName(driver);
        bds.setUrl(url);
        bds.setUsername(username);
        bds.setPassword(password);
        return bds;
    }
}

Upvotes: 0

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

EDITED You have trailing ; in your config. remove it

The exception means the specific class is not found in the classpath.

You need to add the dependency contains the corresponding implemenation for h2:

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

Upvotes: 1

Related Questions