【问题标题】:Cannot connect to MySQL due to incorrect time zone由于时区不正确,无法连接到 MySQL
【发布时间】:2019-06-27 10:57:49
【问题描述】:

我已经制作了一个 java 应用程序,我想在打开窗口时尝试连接到 sql 数据库。我添加了 my-sql-java-8.0.14.jar 连接器,但是当我运行应用程序时,我收到一条错误消息。 我已尝试多次重新连接、重新构建数据库并连接新数据库,但我仍然收到以下错误消息。

提前感谢您的帮助

 "Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Cannot connect to the dbat testDBPack.MainWindow$1.windowOpened(MainWindow.java:47)
    at java.awt.Window.processWindowEvent(Unknown Source)
    at javax.swing.JFrame.processWindowEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.sql.SQLException: The server time zone value '????????? ??? GTB' 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:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at testDBPack.MainWindow$1.windowOpened(MainWindow.java:45)
    ... 25 more
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '????????? ??? GTB' 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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
    at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
    at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2241)
    at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2265)
    at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    ... 31 more
"

我已经检查了我的凭据,我正在运行 sql,但我无法解决问题。

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


import javax.swing.JLabel;
import javax.swing.JSeparator;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

@SuppressWarnings("serial")
public class MainWindow extends JFrame {

    JPanel contentPane;
    static Connection conn;

    public MainWindow() {
        setResizable(false);
        setTitle("Coding Factory");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        setBounds(100, 100, 433, 293);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {

                String url = "jdbc:mysql://localhost:3306/teachers";
                String username = "panos123";
                String password = "panagiotis";

                try {
                    conn = DriverManager.getConnection(url,username,password);
                }catch (SQLException ex) {
                    throw new IllegalStateException("Cannot connect to the db",ex);
                }
            }
        });
        JLabel lblNewLabel_1 = new JLabel("Quality Assistance");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 30));
        lblNewLabel_1.setBounds(71, 32, 295, 37);
        contentPane.add(lblNewLabel_1);

        JButton TeachersButton = new JButton(" ");
        TeachersButton.setBounds(10, 144, 46, 34);
        contentPane.add(TeachersButton);

我还有另一个主课:

package testDBPack;

import java.awt.EventQueue;

public class TeachersApp {
    static MainWindow mainFrame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mainFrame = new MainWindow();
                    mainFrame.setVisible(true);
                    mainFrame.setLocationRelativeTo(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

【问题讨论】:

  • 这似乎是关键:引起:com.mysql.cj.exceptions.InvalidConnectionAttributeException:服务器时区值'????????? ??? GTB' 无法识别或代表多个时区。如果您想利用时区支持,您必须配置服务器或 JDBC 驱动程序(通过 serverTimezone 配置属性)以使用更具体的时区值。

标签: java mysql sql database timezone


【解决方案1】:

时区未配置。尝试使用以下网址:

jdbc:mysql://localhost/teachers?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

【讨论】:

  • 你好时区消失的错误消息,但现在我仍然得到我无法连接到数据库以及用户访问被拒绝:线程“AWT-EventQueue-0”java.lang.IllegalStateException中的异常:无法连接到 testDBPack.MainWindow$1.windowOpened(MainWindow.java:47) 处的数据库,然后:原因:java.sql.SQLSyntaxErrorException:用户'panos123'@'%' 拒绝访问 com 上的数据库'db'。 mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
  • 嗯,是的,“db”应该替换为数据库名称,现在试试
  • 如果这个答案对您有帮助,请考虑将其标记为“正确”,以便遇到相同问题的每个人都知道。
  • 嘿,它有帮助,但没有解决问题,解决方案是下载 mysql-connector-java-5.1.47 以便在我的电脑上工作,
  • 但是问题中的错误已经解决了^^如果还有其他不相关的问题-没关系。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-07
  • 2020-05-22
  • 1970-01-01
  • 1970-01-01
  • 2014-05-25
  • 2021-10-07
相关资源
最近更新 更多