在使用mysql时遇到的一些小问题汇总(新手村问题)

java使用mysql报异常汇总

.
.

1、首先要知道的是自己的mysql的版本号,我这里使用的是8.0.11版本的mysql

对应的版本号最好要使用对应的mysql驱动jar包,例如:

Exception in thread "main" java.sql.SQLException: Unknown system variable 'query_cache_size'

运行时会出现这样的异常,此异常就是代表驱动jar包的版本与mysql的版本不兼容
解决办法:更换与mysql版本兼容的驱动jar包
.
.

2、注册驱动时的错误

显示下面异常

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这个驱动已经不能用了,要按照提示用新的驱动。 5.几版本的mysql好像是没有这个问题的,高版本的因为更新的较多的方法所以需要用新提供的驱动。
解决方法:依照提示的信息使用新的驱动。如提示中显示The new driver class is `com.mysql.cj.jdbc.Driver’ 则将原本的驱动替换成为com.mysql.cj.jdbc.Driver即可

3、时区的错误

由于mysql内置使用的是0时区,我们中国这边则是东八区
刚安装好的mysql需要修改时区,否则会报以下异常

The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrec

解决方法1:直接在mysql执行语句:
set global time_zone=’+8:00’

.
解决方法2:修改配置文件
找到my.ini, 在mysqld 下增加 default-time-zone = ‘+8:00’
必须放到mysqld 下,放入其它位置无效

.
.

4、SSL连接问题

我们经常看到一下错误


Sat Jul 25 13:25:02 CST 2020 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.
1
Sat Jul 25 13:25:04 CST 2020 WARN: Caught while disconnecting...

EXCEPTION STACK TRACE:



** BEGIN NESTED EXCEPTION ** 

javax.net.ssl.SSLException
MESSAGE: closing inbound before receiving peer's close_notify

STACKTRACE:

javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
	at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:133)
	at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:307)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:263)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:254)
	at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:645)
	at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:624)
	at com.mysql.cj.protocol.a.NativeProtocol.quit(NativeProtocol.java:1284)
	at com.mysql.cj.NativeSession.quit(NativeSession.java:182)
	at com.mysql.cj.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:1911)
	at com.mysql.cj.jdbc.ConnectionImpl.close(ConnectionImpl.java:730)
	at com.lele.demo.jdbc_demo1.main(jdbc_demo1.java:20)


** END NESTED EXCEPTION **



Process finished with exit code 0

虽说这对初学者程序的运行没有多大的影响,但是看到那么一大段的错误提示就非常的烦。

原因是使用高版本的mysql时需要指明SSL连接
解决方法:在获取数据库连接对象时的url后面加上?useSSL=true或者?useSSL=false
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_book?useSSL=false", "xx", "xx");

加上后异常就会解决
.
.
.
.
.
.
以上为本人学习时的记录或遇到的问题,如有侵权,请联系立即删除

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值