How to avoid the url Time Zone traps in mysql

Source: Internet
Author: User

How to avoid the url Time Zone traps in mysql

Preface

Recently, when using mysql's jar above 6.0.x, You need to specify serverTimezone in the url of the Code. An exception occurs:

1. serverTimezone not specified

Configure url in xml

<property name="url" value="jdbc:mysql://localhost:3306/mybatisstudy"/>

Exception

Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: 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.

You must configure the server or JDBC Driver (through the serverTimezone Configuration Attribute). If you want to use time zone support, you need to use a more detailed time zone value.

2. Online Solutions

Add a parameter after the url? ServerTimezone = utc

<property name="url" value="jdbc:mysql://localhost:3306/springdatastudy?serverTimezone=UTC"/>

. Problems Encountered

Although there is no error in the time zone program added above, we encountered a problem when inserting data into the database using java code.

For example, the insert time in the java code is 17:29:56.

However, the time displayed in the database is: 09:29:56

3. Root Cause

Because of time zone settings.

UTC indicates the global standard time, but we use the Beijing Time Zone (UTC + 8), which is eight hours ahead of UTC.

UTC + (+ 0800) = Local (Beijing) Time

4. Solution

The Time Zone of the url uses the standard Chinese time. The same is true.serverTimezone=Asia/Shanghai

4.1 Use java code to obtain the local time zone id

Calendar cal = Calendar.getInstance();TimeZone timeZone = cal.getTimeZone();System.out.println(timeZone.getID());System.out.println(timeZone.getDisplayName());
Asia/Shanghai China Standard Time

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.