MySQL 驱动报错 jdbc.Driver

连接数据库代码:

      String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123456";
        Class.forName("com.mysql.jdbc.Driver");

        Connection connection = DriverManager.getConnection(url, username, password);


       

一开始写完上面这几行代码,先报了这个错误:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

这个错误就是字面意思,`com.mysql.cj.jdbc.Driver'代替了旧的 `com.mysql.jdbc.Driver',然后这个驱动是自动加载的,所以 Class.forName("com.mysql.jdbc.Driver");这句代码可以直接注释掉

再运行,又报了下面这个错误:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc::mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8

上面已经改掉了驱动的错误,所以这里还是无法加载,那就大概率是url的问题了,尝试了网上不同的方法,最后的解决办法是把:

jdbc::mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8

改成

jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8

去掉了jdbc后的一个冒号,上面那行是5.x版本的写法,下面这行才是8.x版本的写法。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值