一、在使用mysql的jdbc驱动最新版(6.0+)版本时驱动方式更改
MySQL5:com.mysql.jdbc.Driver
MySQL6+:com.mysql.cj.jdbc.Driver
否则会出现以下错误: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.
二、在使用mysql的jdbc驱动最新版(6.0+)版本时,数据库和系统时区差异引起的问题
MySQL5:jdbc:mysql://localhost:3306/库名
MySQL6+:jdbc:mysql://localhost:3306/库名?serverTimezone=UTC&characterEncoding=utf-8
否则会出现以下错误: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.
三、正确示范
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 测试跟数据库建立连接
* @author 18072
*
*/
public class Demo01 {
public static void main(String[] args) {
//加载驱动类
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//建立连接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/pipi?serverTimezone=UTC&characterEncoding=utf-8","root","123456");
System.out.println("连接成功!!!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
四、最简单的解决方案(不推荐)
更换5.5版本的MySQL,操作简单,但是作为新时代的程序猿还是得与时俱进,紧跟步伐!!!
我是一个疯狂踩坑的程序猿Pipi酱~