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= 
  • can u please show the code which is not working? and not the one which is working – Abhinaba Chakraborty Jul 23 '20 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. – Fullslack Jul 23 '20 at 12:02
  • Yeah right, @Fullslack.dev, that's exactly my problem. – user3819290 Jul 23 '20 at 12:07
  • @user3819290 first of all , have u checked if those property placeholders are even working? – Abhinaba Chakraborty Jul 23 '20 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. – user3819290 Jul 23 '20 at 12:14
  • https://stackoverflow.com/questions/36826999/could-not-load-jdbc-driver-class-com-mysql-jdbc-driver maybe this can help solve the issue – Fullslack Jul 23 '20 at 12:14
  • @Fullslack.dev, how if I'm not using a maven that includes pom.xml? I'm just using a regular java project for this. – user3819290 Jul 23 '20 at 12:16
  • Where is your MySQL driver jar located? Because like Stefan Zhelyazkov below you need to load the driver somewhere. It's not appearing like magic into your application and by default Spring has no JDBC drivers loaded. Also this is not a Spring application because you don't have the proper annotations for it. – Fullslack Jul 23 '20 at 12:20
  • Try `Class.forName(this.driver).newInstance()` – Abhinaba Chakraborty Jul 23 '20 at 12:22
  • @Fullslack.dev, it is located at the referenced library, alongside with the JARS needed for spring. What do you mean proper annotations? – user3819290 Jul 23 '20 at 12:50
  • @AbhinabaChakraborty, adding .newInstance() isn't working. – user3819290 Jul 23 '20 at 12:50

1 Answers1

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

Stefan Zhelyazkov
  • 2,599
  • 4
  • 16
  • 41
  • Hi, I'm not using maven or gradle for this one. Just a regular java project. – user3819290 Jul 23 '20 at 12:06
  • 1
    You will then need to add the jar to the classpath for your project. If you're using IntelliJ, this answer will be helpful: https://stackoverflow.com/a/1051705/9698467 You can get the jar from this link: https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.21/mysql-connector-java-8.0.21.jar – Stefan Zhelyazkov Jul 23 '20 at 17:11