java.sql.SQLException: com.mysql.cj.jdbc.Driver

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

这篇文章分享一下Springboot整合Elasticsearch时遇到的一个问题,项目正常启动,但是查询数据库的时候发生了一个异常java.sql.SQLException: com.mysql.cj.jdbc.Driver

java.sql.SQLException: com.mysql.cj.jdbc.Driver
	at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:625) ~[druid-1.1.21.jar:1.1.21]
	at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:897) [druid-1.1.21.jar:1.1.21]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1376) [druid-1.1.21.jar:1.1.21]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1372) [druid-1.1.21.jar:1.1.21]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:109) [druid-1.1.21.jar:1.1.21]
	at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) [spring-jdbc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) [spring-jdbc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) [spring-jdbc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:80) [mybatis-spring-2.0.7.jar:2.0.7]
	at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67) [mybatis-spring-2.0.7.jar:2.0.7]
	at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:337) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) [mybatis-3.5.9.jar:3.5.9]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427) [mybatis-spring-2.0.7.jar:2.0.7]
	at com.sun.proxy.$Proxy56.selectList(Unknown Source) [na:na]
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224) [mybatis-spring-2.0.7.jar:2.0.7]
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:145) [mybatis-3.5.9.jar:3.5.9]
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86) [mybatis-3.5.9.jar:3.5.9]
	at com.sun.proxy.$Proxy57.selectAll(Unknown Source) [na:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) [spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) [spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) [spring-tx-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) [spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at com.sun.proxy.$Proxy58.selectAll(Unknown Source) [na:na]
	at com.example.elasticsearch.controller.SongController.list(SongController.java:28) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) [tomcat-embed-core-9.0.38.jar:4.0.FR]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) [tomcat-embed-core-9.0.38.jar:4.0.FR]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-embed-websocket-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_152]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_152]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_152]
Caused by: java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_152]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_152]
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_152]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_152]
	at java.lang.Class.forName0(Native Method) ~[na:1.8.0_152]
	at java.lang.Class.forName(Class.java:264) ~[na:1.8.0_152]
	at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:623) ~[druid-1.1.21.jar:1.1.21]
	... 93 common frames omitted

通过断点发现加载数据库驱动的时候传进来的类加载器居然是null,惊呆了我的小伙伴,不知道看这篇文章的童鞋有没有遇到过这种问题,反正我是第一次遇到。

java.sql.SQLException: com.mysql.cj.jdbc.Driver,java,mysql

最后发现原因让人哭笑不得,居然是没有导入jdbc的依赖。

最后在pom.xml文件中添加依赖,然后刷新Maven,问题就解决了。

    <properties>
        <java.version>1.8</java.version>
        <mysql.version>8.0.28</mysql.version>
    </properties>
  
    <dependencies>        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
    <dependencies>

总结:以前会直接把其他项目上的依赖批量复制过去,这次因为整合elasticsearch,只是想把mysql里的数据导入elasticsearch,所以用不到那么多依赖,因此是一个个复制的,mybatis的都复制过去了,但是把mysql给忘了。文章来源地址https://www.toymoban.com/news/detail-691179.html

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

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

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

相关文章

  • java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver 异常解决

    java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver 异常解决

      目录 问题 原因 解决         今天在开发一个DB同步工具的时候,因为采用的是原生的jdbcDriver链接数据库的,代码如下“ Class.forName(this.jdbcDriver); ”,我这边的配置是“ \\\"jdbcDriver\\\":\\\"com.mysql.cj.jdbc.Driver\\\" ”,当程序走到这一行的时候就报错“ java.lang.ClassNotFoundException: com.mys

    2024年02月08日
    浏览(45)
  • java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver解决方案

    java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver解决方案

    此报错会出现在两种情况,有各自的解决方法。 如果在Java程序中报错 ,那么我们就参考 方法1 (单Java程序),错误演示: 如果你是在Tomcat中报错 ,那么我们可以参考 方法2 (Tomcat项目),错误演示:   错误原因:其实就是程序没有安装JDBC驱动,对于Java程序来说我们只需

    2024年02月08日
    浏览(45)
  • java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver报错问题分析

    java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver报错问题分析

    java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver 这个错误通常意味着应用程序服务器在尝试在只读模式下访问数据库时出现了问题,该错误与事务处理有关。通常出现在JDBC连接对象或事务对象的创建或状态查询的过程中。 在JDBC中, 只读事务需要从数据库检索数据 ,但是不能进

    2024年02月11日
    浏览(11)
  • 报错 com.alibaba.druid.pool.DruidDataSource error严重:java.sql.SQLException: ${jdbc.driver}

    报错 com.alibaba.druid.pool.DruidDataSource error严重:java.sql.SQLException: ${jdbc.driver}

    解决办法 pom.xml中提高  Spring核心依赖版本 修改前: 修改后 成功解决 可能原因 修改之前是这样的 mysql驱动版本高于spring核心  修改后 spring核心版本高于mysql驱动  

    2024年02月03日
    浏览(11)
  • 使用IDEA时遇到java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver报错的解决方案

    使用IDEA时遇到java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver报错的解决方案

    先检查项目lib文件夹下有没有mysql的jar包,没有就把jar包复制到该目录下 再检查项目结构中有没有导入mysql的jar包,如果没有导入后记得点击OK。 最后检查你的tomcat的lib目录下有没有mysql的jar包,没有就把该jar包复制到该目录下。 最后重启IDEA即可    mysql 8.0以上 的版本使用

    2024年02月03日
    浏览(23)
  • 【Spring中MySQL连接错误】Cannot load driver class: com.mysql.cj.jdbc.Driver

    【Spring中MySQL连接错误】Cannot load driver class: com.mysql.cj.jdbc.Driver

    Caused by: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method ‘dataSource’ threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver 这类报错是因为mysql-connector-java

    2024年02月13日
    浏览(44)
  • 处理JDBC:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/DB

    处理JDBC:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/DB

    今天花费一个多小时的事件处理这个问题,最终的处理方法真的是太…棒了,我重开了个项目,建了下模块,结果问题解决了。 本着斩草要除根……咳咳,刨根问底的原则,我决定细查一下这个问题,下面是我得到的结果! 不说废话,简单来说:   官方定义了的一套操作

    2023年04月11日
    浏览(13)
  • DBeaver报错:can‘t load driver class ‘com.mysql.cj.jdbc.Driver‘

    DBeaver报错:can‘t load driver class ‘com.mysql.cj.jdbc.Driver‘

    如图: Can\\\'t create driver instance   Error creating driver \\\'MySQL\\\' instance. Most likely required jar files are missing. You should configure jars in driver settings. Reason: can\\\'t load driver class \\\'com.mysql.cj.jdbc.Driver\\\'   Error creating driver \\\'MySQL\\\' instance. Most likely required jar files are missing. You should configure jars in driver settin

    2024年02月15日
    浏览(54)
  • DBeaver驱动连接错误解决方法(提示:com.mysql.cj.jdbc.Driver错误)

    DBeaver驱动连接错误解决方法(提示:com.mysql.cj.jdbc.Driver错误)

    1、提示驱动错误且下载一直失败         1、打开DBeaver点击窗口--窗口--首选项--点击驱动--Maven--添加新的源         2、添加源地址然后进行应用,把添加的源地址移动到最上面(我把其他的地址禁用了,我用不了)         阿里:http://maven.aliyun.com/nexus/content/groups/publ

    2024年02月05日
    浏览(47)
  • driver-class-name: com.mysql.cj.jdbc.Driver代码爆红的解决问题

    driver-class-name: com.mysql.cj.jdbc.Driver代码爆红的解决问题

    在搭建运行springboot项目时,数据库驱动添加会报错。 而在此之前需要在pom.xml中添加mysql依赖,在多次寻求问题解决方法的过程中,我发现一个关键的说辞,就是: 我在mysql依赖中添加了版本号,而这个问题的出现很可能就是版本号不一致,导致了本次标红的主题: 那么到了

    2024年02月06日
    浏览(49)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包