5.7版本以上的MySQL常见问题解决!

本文主要介绍了在使用MySQL 5.7及以上版本时遇到的两个常见问题及解决方案。首先,由于驱动包名改变,需要更新jdbc驱动注册。其次,在创建数据库连接时,需要在URL中指定SSL安全、字符编码和时区设置,以避免连接问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、注册驱动

在学习jdbc过程中遇到以下常见问题,都是与数据库版本有关,MySQL5.7以上的新版本好多地方进行了修改,所以现在对遇到的问题进行展示解决:

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.com.mysql.cj.jdbc.Driver,所以ji将注册的

Class.forName("com.mysql.jdbc.Driver");

改为

Class.forName("com.mysql.cj.jdbc.Driver");

然后运行错误消失。

2、获得数据库连接

获得数据库连接的url,以前的版本不用关心ssl安全、编码与时区问题,而新版本在url中需要加入ssl安全、编码与与时区:

Mon Aug 13 21:03:53 CST 2018 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.
java.sql.SQLException: 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.
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:832)
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at com.itcast.demo.JDBCDemo.main(JDBCDemo.java:45)

未加入ssl安全、编码与时区问题

String url = "jdbc:mysql://localhost:3306/mybase";

加入后

String url = "jdbc:mysql://localhost:3306/mybase?characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";

然后注册与获取数据库连接问题就完美解决了。

try {
			//Class.forName("com.mysql.jdbc.Driver");
			Class.forName("com.mysql.cj.jdbc.Driver");
			//获得数据库连接
			//String url = "jdbc:mysql://localhost:3306/mybase";
			String url = "jdbc:mysql://localhost:3306/mybase?characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
			String username = "root";
			String password = "root123";
			Connection con = DriverManager.getConnection(url, username, password);
			System.out.println(con);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
com.mysql.cj.jdbc.ConnectionImpl@6956de9

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值