JDBC数据库连接时发生时区错误

在使用IDEA学习JDBC时,连接数据库遇到了这样一个问题
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原始设定时区是美国时区

`

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Demo01 {
    public static void main(String[] args) {
        //加载驱动类
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            try {
                     Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "");
                System.out.println(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

上面是原始代码
加入这个语句 ?serverTimezone=UTC 经过修改后能够运行了

`import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
//测试和数据库建立连接
public class Demo01 {
    public static void main(String[] args) {
        //加载驱动类
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            try {
                //建立连接
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?serverTimezone=UTC ", "root", "");
                System.out.println(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

`

上一篇:jdbc插入mysql中文乱码或??问题解决


下一篇:JavaWeb(九)JDBC——JDBC小工具