Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver

这篇具有很好参考价值的文章主要介绍了Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、报错问题

Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver
caused by: java.lang.classnotfoundexception: could not load requested class,常见报错问题,java,mysql,hibernate

二、问题背景

新建Java项目,并添加 Hibernate 框架支持,启动测试(运行默认的Main类中的main()方法),出现报错。

Main.java

import org.hibernate.HibernateException;
import org.hibernate.Metamodel;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javax.persistence.metamodel.EntityType;

import java.util.Map;

public class Main {
    private static final SessionFactory ourSessionFactory;

    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            ourSessionFactory = configuration.buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
        final Session session = getSession();
        try {
            System.out.println("querying all the managed entities...");
            final Metamodel metamodel = session.getSessionFactory().getMetamodel();
            for (EntityType<?> entityType : metamodel.getEntities()) {
                final String entityName = entityType.getName();
                final Query query = session.createQuery("from " + entityName);
                System.out.println("executing: " + query.getQueryString());
                for (Object o : query.list()) {
                    System.out.println("  " + o);
                }
            }
        } finally {
            session.close();
        }
    }
}

项目结构如下图所示:
caused by: java.lang.classnotfoundexception: could not load requested class,常见报错问题,java,mysql,hibernate

三、原因分析

缺少mysql对应版本的驱动包。

四、解决方案

步骤1:去官网下载对应版本的驱动,然后解压得到jar包(本文以下载5.1.47版本为例)

官网下载对应版本的驱动(jar包)地址:https://downloads.mysql.com/archives/c-j/

caused by: java.lang.classnotfoundexception: could not load requested class,常见报错问题,java,mysql,hibernate

下载后的文件:mysql-connector-java-5.1.47.zip
caused by: java.lang.classnotfoundexception: could not load requested class,常见报错问题,java,mysql,hibernate

解压文件,得到jar包:mysql-connector-java-5.1.47.jar

caused by: java.lang.classnotfoundexception: could not load requested class,常见报错问题,java,mysql,hibernate

步骤2:将 jar 包导入项目

caused by: java.lang.classnotfoundexception: could not load requested class,常见报错问题,java,mysql,hibernate

caused by: java.lang.classnotfoundexception: could not load requested class,常见报错问题,java,mysql,hibernate

选择 mysql-connector-java-5.1.47.jar,如下图所示。
caused by: java.lang.classnotfoundexception: could not load requested class,常见报错问题,java,mysql,hibernate

添加 mysql-connector-java-5.1.47.jar 之后,如下图所示。然后点击“OK”按钮即可。
caused by: java.lang.classnotfoundexception: could not load requested class,常见报错问题,java,mysql,hibernate文章来源地址https://www.toymoban.com/news/detail-784092.html

到了这里,关于Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Caused by: java.lang.ClassNotFoundException: org.apache.maven.exception.ExceptionHandler 的解决办法

    出现这个问题,是由于开发环境迁移,在迁移的过程中操作不规范导致的, 由一台开发服务器,迁移至另外一台开发服务器时,启动失败, 错误提示:“  Exception in thread \\\"main\\\" java.lang.NoClassDefFoundError: org/apache/maven/exception/ExceptionHandler ” 具体内容如下:  如下图: 关于这个

    2024年02月04日
    浏览(26)
  • 【解决】Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=99

    问题出现场景 此情况出现在Android程序将相机所拍照片存至手机的过程,主要原因是存储照片的操作反馈的数据为空,在代码中没有合理处理的原因。当使用APP时,出现了闪退现象。究于此,文章进行问题分析和解决。 出现问题的代码 相机拍照请求代码: 拍照后结果处理代

    2024年02月03日
    浏览(24)
  • 解决 Could not write request: no suitable HttpMessageConverter found for request type [java.lang.Long]

    业务服务通过RestTemplate调用文件上传服务。( java.version1.8/java.version spring.cloud.versionHoxton.SR12/spring.cloud.version spring.cloud.alibaba.version2.2.9.RELEASE/spring.cloud.alibaba.version spring.boot.version2.3.12.RELEASE/spring.boot.version ) 由于restTemplate中引入了FormHttpMessageConverter消息转换器,在调用过程中

    2024年02月04日
    浏览(43)
  • 解决Caused by: java.lang.IllegalStateException:

    目录 问题描述 解决方案 1. 修改JVM的-Xss参数 2. 检查循环继承依赖项 3. 更新相关库和依赖 总结 在使用Java开发Web应用程序时,我们有时会遇到​ ​Caused by: java.lang.IllegalStateException​ ​​异常,其中包含一个关于StackOverflow错误的描述。这种错误可能会导致无法完成对Web应用程

    2024年02月03日
    浏览(37)
  • 出现Caused by: java.lang.NullPointerException异常的解决方法

    拿到老师的项目,项目启动时正常,网页提交数据时出现Caused by: 报错, 上一条日志显示 是连接mysql的时候出现bug导致的, 查了一下是数据库版本不匹配问题, 于是我看了一下自己电脑的mysql版本 哇哦,是mysql8.0,再看一下依赖里的mysql版本 是mysql5.1.32,这俩版本不匹配,

    2024年02月11日
    浏览(80)
  • Caused by: java.lang.IllegalStateException: No DataSource set

    如果Nacos连不上数据库,还哪都配置正确,请注意时区 serverTimezone=Asia/Shanghai

    2024年02月11日
    浏览(57)
  • Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not find me.jessyan:aut

    1.如果在Android Studio中运行项目,下载依赖包,出现报错内容如下 2.如果构建项目时拉取不到gradle资源,可以将地址修改成阿里云的国内镜像。解决方式如下: 重新在sync Now就可以成功

    2024年02月04日
    浏览(30)
  • Caused by: java.lang.IllegalStateException: Failed to introspect Class

    1、问题报错如下: 2、问题解析: 意思是:未能反射rabbitmq类包(就是添加了依赖没加载进去);需与ClassNotFindException的错误区分,此异常是引用了没有依赖的类。 3、解决方案: 方案一(如果导入的依赖“scope”标签为值provided,则修改如下): 改为如下所示:(去掉scop

    2024年02月11日
    浏览(30)
  • Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.C

    最近在学习JDK17的时候遇到这么一个问题,springBoot启动失败,日志如下: 原因:这是由于 JDK 8 中有关反射相关的功能自从 JDK 9 开始就已经被限制了,为了兼容原先的版本,需要在运行项目时添加  --add-opens java.base/java.lang=ALL-UNNAMED  选项来开启这种默认不被允许的行为。 解

    2024年02月03日
    浏览(82)
  • 解决Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘inteController‘

    出现这个错也可能会出现项目无法启动的错误。完整错误是 一、错误出现原因 :         controller层出现了重复的方法映射                  可以看到图片中两个方法的映射是一样的,这是问题所在 解决方法也很简单,既然已经知道了问题所在那么解决起来也很简单

    2024年02月10日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包