Mysql客户端工具能够链接,可是代码访问就会报错

在执行以下代码的时候,客户端工具是能够链接的:html

 

可是用代码去访问的时候,却老是报错误:java

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.
    com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
    com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
    com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
    com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
    com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
    com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
    com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
    com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
    com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
    com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
--------------------- 
做者:Going_cc 
来源:CSDN 
原文:https://blog.csdn.net/weixin_39033443/article/details/81711306 
版权声明:本文为博主原创文章,转载请附上博文连接!mysql

代码链接mysql的demo以下:sql

public static void main(String[] args) {
    
    Connection conn = null;
    Statement stmt = null;
    try{

        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/activiti";
        String dbName = "root";
        String password = "123456";

        // 注册 JDBC 驱动
        Class.forName(driver);

        // 打开连接
        System.out.println("链接数据库...");
        conn = DriverManager.getConnection(url,dbName,password);

        // 执行查询
        System.out.println(" 实例化Statement对象...");
        stmt = conn.createStatement();
        String sql;
        sql = "SELECT rid, rolename FROM role";
        ResultSet rs = stmt.executeQuery(sql);

        // 展开结果集数据库
        while(rs.next()){
            // 经过字段检索
            int rid  = rs.getInt("rid");
            String rolename = rs.getString("rolename");

            // 输出数据
            System.out.print("ID: " + rid);
            System.out.print(", 站点名称: " + rolename);
            System.out.print("\n");
        }
        // 完成后关闭
        rs.close();
        stmt.close();
        conn.close();
    }catch(SQLException se){
        // 处理 JDBC 错误
        se.printStackTrace();
    }catch(Exception e){
        // 处理 Class.forName 错误
        e.printStackTrace();
    }finally{
        // 关闭资源
        try{
            if(stmt!=null) stmt.close();
        }catch(SQLException se2){
        }// 什么都不作
        try{
            if(conn!=null) conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }
    }
    System.out.println("Goodbye!");
}

后经查询,引入以下方法解决:数据库

https://www.cnblogs.com/ljy-20180122/p/9157912.htmlapp

即执行命令:工具

show variables LIKE '%time_zone%';url

set global time_zone = '+8:00';spa

话很少说,从错误便可知道是时区的错误,所以只要将时区设置为你当前系统时区便可,.net

   所以使用root用户登陆mysql,按照以下图所示操做便可。

       

     我电脑的系统为北京时区,所以在系统中设置后,再链接数据库运行,一切OK!

相关文章
相关标签/搜索