Class 未找到异常 org//h2//驱动程序

Class Not Found Exception org//h2//Driver

我正在学习 Spring 框架并尝试从 .properties 文件中注入属性。

这是我的 .properties 文件

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

当我试图将 driver 字段传递给 Class.forName(drive) 时,程序无法连接到 数据库并抛出一个 java.lang.ClassNotFoundException: org/h2/Driver; 但它将 driver 变量打印为“org.h2.Driver”到控制台就好了。 My console screenshot

我也运行这个程序Class.forName("org.h2.Driver"),它运行很好;但是,当我用 driver 替换它时,它不起作用

这是我的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 + '\'' +
                '}';
    }
}

已编辑 您的配置中有尾随 ;。删除它

异常意味着在 class 路径中找不到特定的 class。

您需要添加包含h2对应实现的依赖:

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

您应该完成以下步骤以将 H2 数据库配置到您的 Spring MVC 应用程序。

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