java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

已回答

I have added the mysql jar file to the project library but i still get the below error:

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)

I added the jar in my directory in my project. I added it to the project structure library. I added it to the dependencies. Nothing works. The program cannot access the database. 

Here's the code I have so far:

db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eventmaster
jdbc.user=root
jdbc.password=password

DatabaseConfig.java

package config;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class DatabaseConfig {

    private static Connection connection = null;

    public static Connection getConnection() {
        if (connection != null) {
            return connection;
        } else {
            try {
                // Load database properties
                InputStream inputStream = DatabaseConfig.class.getClassLoader().getResourceAsStream("db.properties");
                Properties properties = new Properties();
                properties.load(inputStream);

                // Get properties
                String driver = properties.getProperty("jdbc.driver");
                String url = properties.getProperty("jdbc.url");
                String user = properties.getProperty("jdbc.user");
                String password = properties.getProperty("jdbc.password");

                // Register JDBC driver
                Class.forName(driver);

                // Open a connection
                connection = DriverManager.getConnection(url, user, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return connection;
        }
    }
}

 

Main.java

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();
        }
    }
}

 

 

0
Please upload the sample project to https://uploads.jetbrains.com/, and paste their upload IDs here and we will investigate it, thank you!
0

请先登录再写评论。