java - 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=
解决方案
com.mysql.jdbc.Driver
当找不到该类时,意味着它在运行时不可用。请检查您的build.gradle
or中是否有 MySQL java 连接器pom.xml
,如果没有,请添加:
https ://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.21
推荐阅读
- sql - SQL 乘以 AVG
- java - 下拉单击时 Android Webview 崩溃
- image-processing - Keras ImageDataGenerator 将值重新缩放为 [-0.5, 0.5]
- python - project euler #4 使用 python 的最大回文
- javascript - 如何使用单个键盘快捷键复制和粘贴 Electron 中的文本?
- javascript - 导入到 vue.js 组件中
- postgresql - PG 日志:无法(重新)获取截断扫描的排他锁
- arrays - Joi 验证正则表达式或模式
- reactjs - 如何使用 Mobx 和 react-nativation 在 react native 项目中解决这个错误?
- codeigniter - CodeIgniter 的发送邮件功能在本地主机上有效,但在实时服务器上无效