1

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= 
11
  • can u please show the code which is not working? and not the one which is working Commented Jul 23, 2020 at 11:58
  • @AbhinabaChakraborty the code listed is not working. The wording might not be very clear, but the OP tried to run everything from inside the main method and that worked. However the current code uses a bean and external class and that isn't working. Commented Jul 23, 2020 at 12:02
  • Yeah right, @Fullslack.dev, that's exactly my problem. Commented Jul 23, 2020 at 12:07
  • @user3819290 first of all , have u checked if those property placeholders are even working? Commented Jul 23, 2020 at 12:12
  • @AbhinabaChakraborty, yes. I can test if property placeholders are working through the displayConnection() method. In my case, I can fetch the values. Commented Jul 23, 2020 at 12:14

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I'm not using maven or gradle for this one. Just a regular java project.
You will then need to add the jar to the classpath for your project. If you're using IntelliJ, this answer will be helpful: stackoverflow.com/a/1051705/9698467 You can get the jar from this link: repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.21/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.