You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已添加MySQL驱动JAR包,IntelliJ IDEA仍报ClassNotFoundException求助

问题:IntelliJ IDEA中运行MySQL连接程序报ClassNotFoundException错误

运行连接MySQL的Java程序时出现以下错误:

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)

相关代码

db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database
jdbc.user=user
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();
        }
    }
}

已尝试的解决方案(均无效)

  • 按照IntelliJ IDEA的Database Tools and SQL文档操作,测试连接成功,但运行Main.java仍报错
  • 将MySQL JAR包放入项目目录并添加为模块依赖
  • 下载最新版mysql-connector-j.8.3.0 JAR包,添加为模块依赖(尝试过压缩包和解压后的文件)
  • 将本地JAR包添加为项目库
  • 将下载的最新版JAR包添加为项目库
  • 从Maven引入com.mysql:mysql-connector-j:8.3.0库
  • 参考StackOverflow上关于Java连接MySQL数据库的帖子的所有解决方案

内容的提问来源于stack exchange,提问作者Kelli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 12:05:28