Java-MySQL JDBC driver: com.mysql.cj.jdbc.Driver is not loading

Joined
Feb 15, 2025
Messages
2
Reaction score
0
I am trying to connect to MySQL database (MySQL 8.0) using JDK11.
I have downloaded mysql-connector-j-9.1.0.jar and added it to the Path environment variable.
However, when I run my program, I keep getting the error:
"
Error loading MySQL JDBC driver: com.mysql.cj.jdbc.Driver

"
Below is the code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySQLJava {
public static void main(String[] args) {
String dbUrl = "jdbc:mysql://localhost:3306/mydb";
String username = "root";
String password = "";

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(dbUrl, username, password);
System.out.println("Connected to MySQL database!");
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Error loading MySQL JDBC driver: " + e.getMessage());
} catch (SQLException e) {
System.out.println("Error connecting to MySQL database: " + e.getMessage());
} }}

Is there anything that I'm not doing right please?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,249
Messages
2,571,245
Members
47,881
Latest member
eialtagu

Latest Threads

Top