JAVA-SPRING – Error java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver" Upon Putting My Connection to Other Class but working on the Main

huangapple go评论145阅读模式
英文:

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: &quot;com.mysql.jdbc.Driver&quot;

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(&quot;${mysql.driver}&quot;)
	private String driver;
	
	@Value(&quot;${mysql.url}&quot;)
	private String url;
	
	@Value(&quot;${mysql.password}&quot;)
	private String password;
	
	@Value(&quot;${mysql.username}&quot;)
	private String username;
	
	public void displayConnection(){
		System.out.println(&quot;Driver: &quot; + this.driver + &quot;\nURL: &quot;+ this.url + &quot;\nUsername&quot; + this.username + &quot;\nPassword&quot; + this.password);
	}
	
	
	public void connectToDB() throws SQLException, ClassNotFoundException {
		Class.forName(this.driver);
		Connection con = DriverManager.getConnection(url, username, password);
		System.out.println(&quot;Connection has been established&quot;);
	}
}

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(&quot;beans.xml&quot;);
		DBConnection con = context.getBean(&quot;dbconnection&quot;,DBConnection.class);
		con.displayConnection();
		con.connectToDB();
	}
}

connection_details.properties (Values for the database connection)

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

beans.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
    xsi:schemaLocation=&quot;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">
    &lt;context:annotation-config/&gt;
    
    &lt;context:property-placeholder location=&quot;connection_details.properties&quot; /&gt;
	&lt;bean id = &quot;dbconnection&quot; class = &quot;com.jrs.annotation.DBConnection&quot;&gt;
	&lt;/bean&gt;
&lt;/beans&gt;

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.gradlepom.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

huangapple
  • 本文由 发表于 2020年7月23日 19:53:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63053637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定