java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/

刚开始搭建springboot+mybatis+mysql的时候,刚搭好springboot没有配备mysql数据源,但是想先尝试下spring环境有没有问题,直接启动,然后壕无人性的报错了:

***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.

If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
--------------------- 
作者:croder 
来源:CSDN 
原文:https://blog.csdn.net/daxiang52/article/details/79420777 
版权声明:本文为博主原创文章,转载请附上博文链接!

很显然,没有配备数据源,但我在搭建springboot的时候已经选了mysql配置项,启动时候未加载到该数据源,解决方式有两种:

一、如果只是想验证springboot的问题,可在启动类的@EnableAutoConfiguration或@SpringBootApplication中添加exclude

= {DataSourceAutoConfiguration.class},排除此类的autoconfig。启动以后就可以正常运行。

二、添加数据源,在application.xml中添加数据源即可

server:
  port: 8082
  servlet:
    context-path: /customer
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/rfid?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: danny
    driver-class-name: com.mysql.cj.jdbc.Driver

这里需要注意个问题,新版本的mysql驱动包的路径为com.mysql.cj.jdbc.Driver;多了个cj包;

然后建了个表,使用mybatisplus自带的generator包生成相关的实体类、服务类、mapper文件等;但是启动junit的时候又报错了:java.sql.SQLException: No suitable driver found for mysql;

很明显驱动加载不到;仔细检查了路径、root用户、密码及端口号,再三确认觉得没问题啊,还是报错,百度了也大都是这几个问题,有说lib下没导入驱动包的,有说idea配置不太一样的,都照配了还是一样,debug进入源码查看问题就是加载驱动的问题啊

public Connection getConn() {
        Connection conn = null;

        try {
            Class.forName(this.driverName);
            conn = DriverManager.getConnection(this.url, this.username, this.password);
        } catch (SQLException | ClassNotFoundException var3) {
            var3.printStackTrace();
        }

        return conn;
    }

很明显就是路径、root、password三者有的数值不对,驱动包再三确认是加载了的,然后只能从这三者入手,睁大眼睛一个个瞅,终于,发现了一个问题,让我欲哭无泪,果然最安全的地方就是最危险的地方,就是url的路径写错了:

mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useSSL=false

诈一看是不是没问题,开头少了个jdbc。。。

加油吧骚年

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值