英文:
JAVA-SPRING - Error java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver" Upon Putting My Connection to Other Class but working on the Main
问题
在练习 Spring 框架时,我希望将我的 JDBC 连接放在外部类中。然而,在我的主方法中创建对象并调用 `.connectToDB()` 方法时,我不断收到这种错误:
`java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver"`
*这是我的代码:*
**DBConnection.java(具有依赖项的类)**
```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 =
beans.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>
我已经尝试在我的主方法中编写连接代码,它是正常工作的。只有在我将其放在外部类的方法中并在主方法中调用时,我才会收到这种错误。我是否遗漏了什么?
谢谢。
解决方案:
我已经找到了出错的原因,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 =
英文:
While practicing Spring framework, I want my JDBC Connection to be on external class. However, upon creating the object on my main method and calling the .connectToDB()
method, I keep on getting this kind of error:
java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver"
This is my code:
DBConnection.java (Class with dependencies)
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 (My class for main method)
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 (Values for the database connection)
mysql.driver = "com.mysql.jdbc.Driver"
mysql.url = "jdbc:mysql://127.0.0.1:3306/school"
mysql.username= "root"
mysql.password= ""
beans.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>
I've tried to do the connection code on my Main method and it's working. I got only that kind of error when I put it on a method from an external class and call it on my main. Is there anything that I missed to include?
Thank you.
SOLUTION:
I've already found what's going wrong with this, values on the connection_details.properties doesn't need a single or double quote. Thank you for your ideas.
Correct Code for connection_details.properties
mysql.driver = com.mysql.jdbc.Driver
mysql.url = jdbc:mysql://127.0.0.1:3306/school
mysql.username= root
mysql.password=
答案1
得分: 1
当找不到类com.mysql.jdbc.Driver
时,意味着它在运行时不可用。请检查您的build.gradle
或pom.xml
中是否可用MySQL Java连接器,如果没有,请添加它:
https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.21
英文:
When the class com.mysql.jdbc.Driver
is not found, it means that it is not available at runtime. Please check if the MySQL java connector is available in your build.gradle
or pom.xml
, and if it is not, then please add it:
https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.21
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论