java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:391)
at java.base/java.lang.Class.forName(Class.java:382)
at config.DatabaseConfig.getConnection(DatabaseConfig.java:29)
at Main.main(Main.java:8)
The following is the code I'm using:
db.properties
import config.DatabaseConfig;
import java.sql.Connection;
public class Main {
public static void main(String[] args) {
// Initialize database connection
Connection connection = DatabaseConfig.getConnection();
if (connection != null) {
System.out.println("Successfully connected to the database.");
// Perform database operations
} else {
System.out.println("Failed to connect to the database.");
}
// Close the connection when done
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I followed this link: Database Tools and SQL. I was able to connect to my database and successfully run a test connection. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
I put the MySQL jar file in my project directory: RootFolder/MySql.jar then added to my dependencies as follows: file -> Project Structure -> Modules -> Dependencies -> + -> JARs or Directories -> RootFolder/MySql.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
I downloaded the latest MySql.jar file from this link: MySql Community Downloads. The version is mysql-connector-j.8.3.0. I added it to my dependencies as follows: file -> Project Structure -> Modules -> Dependencies -> + -> JARs or Directories -> C:\Users\me\Downloads\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error. (I tried this with zipped and unzipped jar file)
I also tried adding the jar file as a library as follows: file -> Project Structure -> libraries -> + -> Java -> RootFolder/MySql.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
I also tried adding the jar file as a library as follows: file -> Project Structure -> libraries -> + -> Java -> C:\Users\me\Downloads\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
I tried adding the library from Maven as follows: file -> Project Structure -> libraries -> + -> From Maven -> com.mysql:mysql-connector-j:8.3.0. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
I followed ALL of the answers on this stackoverflow post: Connect Java to a MySQL database. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
I get the following error when I try to run a simple program to connect to MySql database in IntelliJ IDEA: [code]java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:391) at java.base/java.lang.Class.forName(Class.java:382) at config.DatabaseConfig.getConnection(DatabaseConfig.java:29) at Main.main(Main.java:8) [/code] The following is the code I'm using: db.properties [code]jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/database jdbc.user=user jdbc.password=password [/code] DatabaseConfig.java [code]package config;
if (connection != null) { System.out.println("Successfully connected to the database."); // Perform database operations } else { System.out.println("Failed to connect to the database."); }
// Close the connection when done try { if (connection != null && !connection.isClosed()) { connection.close(); } } catch (Exception e) { e.printStackTrace(); } } } [/code] [list] [*]I followed this link: Database Tools and SQL. I was able to connect to my database and successfully run a test connection. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
[*] I put the MySQL jar file in my project directory: RootFolder/MySql.jar then added to my dependencies as follows: file -> Project Structure -> Modules -> Dependencies -> + -> JARs or Directories -> RootFolder/MySql.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
[*] I downloaded the latest MySql.jar file from this link: MySql Community Downloads. The version is mysql-connector-j.8.3.0. I added it to my dependencies as follows: file -> Project Structure -> Modules -> Dependencies -> + -> JARs or Directories -> C:\Users\me\Downloads\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error. (I tried this with zipped and unzipped jar file)
[*] I also tried adding the jar file as a library as follows: file -> Project Structure -> libraries -> + -> Java -> RootFolder/MySql.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
[*] I also tried adding the jar file as a library as follows: file -> Project Structure -> libraries -> + -> Java -> C:\Users\me\Downloads\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
[*] I tried adding the library from Maven as follows: file -> Project Structure -> libraries -> + -> From Maven -> com.mysql:mysql-connector-j:8.3.0. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
[*] I followed ALL of the answers on this stackoverflow post: Connect Java to a MySQL database. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.