Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

Java jdbc 无法加载 mysql 驱动

初学 java 连接数据库,正常创建工程并添加 jar,却报错!!!求教!!!

环境:
IDEA: 2020.2
Mysql:8.0.22
系统 macOS BigSur

先贴上目录结构
目录结构

源代码

package com.inmo.main;
public class Main {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

这里报错了,异常就是找不到这个类

BUILD SUCCESSFUL in 250ms
2 actionable tasks: 2 executed
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    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:315)
    at com.inmo.main.Main.main(Main.java:7)
14:56:23: Task execution finished 'Main.main()'.

但我已在 IDEA 中添加这个包
添加的依赖

求教哇!!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

首先你是用的是gradle管理依赖以及编译,所以你需要在gradle的配置文件中配置连接数据库的依赖。怎么配置,需要网上查下,如果没有网络,查询本地gradle添加依赖包。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...