JDBC、MySQL操作中容易出现的问题和navicat连接MySQL出现2059错误

1.   jdbc 连接数据库出现了 No suitable driver found for jdbc ,将驱动包引入lib目录,或者上级导入也无法解决。

        解决办法:

    一:连接URL格式出现了问题(Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/XX","root","XXXX")

    二:驱动字符串出错(com.mysql.jdbc.Driver)

    三:Classpath中没有加入合适的mysql_jdbc驱动

    四:将mysql jdbc 驱动包放进jre的\lib\ext目录下面

2.navicat连接MySQL出现2059错误

    解决办法:
  
  1、进入CMD命令符窗口(win+R)
    2、进入mysql安装目录的bin路径下运行mysql -uroot -p命令进行登录(密码是安装mysql时设置的密码)
    3、输入ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';(root是用户名,localhost是ip地址127.0.0.1都是特指本机,mysql_native_password是旧的密码验证机制,root是密码,最后别忘了分号;)
    4、进入Navicat测试连接
方法二:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #修改加密规则 
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; #更新一下用户的密码 
FLUSH PRIVILEGES; #刷新权限 



3.mysql-connector-java:8.x.x连接jdbc出错的解决方案
    错误原因
  • mysql-connector包使用了新的驱动

  • 连接的URL中需要增加时区信息

  • 错误描述

    1.mysql-connector包使用了新的驱动

    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.jdbc.Driver'驱动类被弃用了,新的驱动类是'com.mysql.cj.jdbc.Driver'。 
    这个驱动会通过SPI的方式自动加载,通常不需要人工手动加载

    SPI – Service Provider Interface(服务提供接口)

    2.连接的URL中需要增加时区信息

    The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents 
    more than one time zone. 
    You must configure either the server or JDBC driver (via the serverTimezone configuration property) 
    to use a more specifc time zone value if you want to utilize time zone support.

    服务器的时区值’�й���׼ʱ��’ (就是乱码,可能因为没有设置一个值)不能被识别,或者该值代表了多个时区。 
    如果你想获得时区支持的功能,你需要用一个更具体的值来设置服务器或JDBC驱动(通过serverTimezone 属性设置)

    另外还有一个警告:

    WARN: Establishing SSL connection without server’s identity verification is not recommended. 
    According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection 
    must be established by default if explicit option isn’t set. 
    For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. 
    You need either to explicitly disable SSL by setting useSSL=false, 
    or set useSSL=true and provide truststore for server certificate verification.

    不推荐不使用服务器身份验证来建立SSL连接。 
    如果未明确设置,MySQL 5.5.45+, 5.6.26+ and 5.7.6+版本默认要求建立SSL连接。 
    为了符合当前不使用SSL连接的应用程序,verifyServerCertificate属性设置为’false’。 
    如果你不需要使用SSL连接,你需要通过设置useSSL=false来显式禁用SSL连接。 
    如果你需要用SSL连接,就要为服务器证书验证提供信任库,并设置useSSL=true。

    SSL – Secure Sockets Layer(安全套接层)


  • 解决方案

    其实报错信息已经很详细的指出了问题的原因,所以根据报错信息解决就行了。

    1.mysql-connector包使用了新的驱动

    解决方法1:jdbc.driver的属性值从com.mysql.jdbc.Driver换为com.mysql.cj.jdbc.Driver
    解决方法2:在代码中去掉Class.forName(driver);,因为驱动会自动加载。

    2.连接的URL中需要增加时区信息

    增加serverTimezone属性并设置时区值,测试UTC(世界标准时间)和GMT(格林威治时间)都可以。

    jdbc.url=jdbc:mysql://localhost:3306/darkbright?serverTimezone=GMT
    • 1

    3.关于SSL连接的Warning

    再在URL后面添加一个属性useSSL,并设置为false

    jdbc.url=jdbc:mysql://localhost:3306/darkbright?serverTimezone=GMT&useSSL=false

  • 未解决的问题

    1. 添加时区信息除了解决报错还有什么用,所谓的Time zone support是什么。
    2. 如果不关闭SSL连接,那如何进行身份验证。





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值