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=