JDBC --导入MySQL驱动,然后试着操作数据库数据
package
然后就悲剧了,看了视频中的代码一遍又一遍,,,,一句。。。。。
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.
Exception in thread "main" 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 specific time zone value if you want to utilize time zone support.
了解到和 和 MySQL的版本有关系:
MySQL5用的驱动url是com.mysql.jdbc.Driver,MySQL6以后用的是com.mysql.cj.jdbc.Driver
然后赶紧看了下自己使用的MySQL 版本是:8.0.17 MySQL
使用的驱动:mysql-connector-java-8.0.22 ,而学习资料中用的5.1.7版本,那问题就找到了
更改 加载驱动为:
Class.forName("com.mysql.cj.jdbc.Driver");
但是仍然有问题
Exception in thread "main" 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 specific time zone value if you want to utilize time zone support.
必须配置服务器或JDBC驱动程序(通过“serverTimezone”配置属性)以使用更特定的时区值。
设置属性:
可以选择东8区的Hongkong、Asia/Shanghai或者Asia/Hongkong作为参数,修改后url为
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true",
"root", "root123");
还有其他的参数设置:
参考MySql 官方资料
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-using-ssl.html