首页 > 解决方案 > JAVA-SPRING - 错误 java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver" 将我的连接连接到其他类但在 Main 上工作

问题描述

在练习 Spring 框架时,我希望我的 JDBC Connection 位于外部类上。但是,在我的主要方法上创建对象并调用该.connectToDB()方法时,我不断收到这种错误:

java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver"

这是我的代码:

DBConnection.java(具有依赖关系的类)

package com.jrs.annotation;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Value;

public class DBConnection {
    @Value("${mysql.driver}")
    private String driver;
    
    @Value("${mysql.url}")
    private String url;
    
    @Value("${mysql.password}")
    private String password;
    
    @Value("${mysql.username}")
    private String username;
    
    public void displayConnection(){
        System.out.println("Driver: " + this.driver + "\nURL: "+ this.url + "\nUsername" + this.username + "\nPassword" + this.password);
    }
    
    
    public void connectToDB() throws SQLException, ClassNotFoundException {
        Class.forName(this.driver);
        Connection con = DriverManager.getConnection(url, username, password);
        System.out.println("Connection has been established");
    }
}

Client.java(我的主方法类)

package com.jrs.annotation;

import java.sql.Connection;
import java.sql.DriverManager;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

    public static void main(String[] args) throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        DBConnection con = context.getBean("dbconnection",DBConnection.class);
        con.displayConnection();
        con.connectToDB();
    }
}

connection_details.properties(数据库连接的值)

mysql.driver  = "com.mysql.jdbc.Driver"
mysql.url     = "jdbc:mysql://127.0.0.1:3306/school"
mysql.username= "root"
mysql.password= ""

豆类.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
    
    <context:property-placeholder location="connection_details.properties" />
    <bean id = "dbconnection" class = "com.jrs.annotation.DBConnection">
    </bean>
</beans>

我尝试在我的 Main 方法上执行连接代码并且它正在工作当我将它放在外部类的方法上并在我的 main 上调用它时,我只得到了那种错误。有什么我错过的吗?

谢谢你。

解决方案: 我已经发现了问题所在,connection_details.properties 上的值不需要单引号或双引号。谢谢你的想法。

connection_details.properties 的正确代码

mysql.driver  = com.mysql.jdbc.Driver
mysql.url     = jdbc:mysql://127.0.0.1:3306/school
mysql.username= root
mysql.password= 

标签: javaspringoop

解决方案


com.mysql.jdbc.Driver当找不到该类时,意味着它在运行时不可用。请检查您的build.gradleor中是否有 MySQL java 连接器pom.xml,如果没有,请添加: https ://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.21


推荐阅读