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?
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?