欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

如何使用Java中的mysql時區(qū)

這篇文章主要講解了如何使用Java中的MySQL時區(qū),內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

創(chuàng)新互聯(lián)專注于社旗網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供社旗營銷型網(wǎng)站建設(shè),社旗網(wǎng)站制作、社旗網(wǎng)頁設(shè)計、社旗網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造社旗網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供社旗網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

前言

話說工作十多年,mysql 還真沒用幾年。起初是外企銀行,無法直接接觸到 DB;后來一直從事架構(gòu)方面,也多是解決問題為主。

這次搭建海外機(jī)房,圍繞時區(qū)大家做了一番討論。不說最終的結(jié)果是什么,期間有同事認(rèn)為 DB 返回的是 UTC 時間。

這里簡單做個驗證,順便看下時區(qū)的問題到底是如何處理。

環(huán)境

openjdk version “1.8.0_242”
mysql-connector-java “8.0.20”
mysql “5.7” 時區(qū) TZ=Europe/London

本地時區(qū) GMT+8

創(chuàng)建個簡單的庫test及表user, 表結(jié)構(gòu)如下:

CREATE TABLE `user` (
 `name` varchar(50) NOT NULL,
 `birth_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1

插入一條測試數(shù)據(jù):

mysql> insert into `user`
  -> values ('Tom', time('2020-05-15 08:00:00'));
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+------+---------------------+
| name | birth_date     |
+------+---------------------+
| Tom | 2020-05-14 08:00:00 |
+------+---------------------+
1 row in set (0.00 sec)

測試代碼:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "root");
Statement stmt = conn.createStatement();
stmt.execute("select * from user where name = 'Tom'");
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
  Timestamp timestamp = rs.getTimestamp("birth_date");
  System.out.println(timestamp.toLocalDateTime().toString());
}

執(zhí)行結(jié)果:

2020-05-14T15:00

分析

程序的執(zhí)行過程同時用 wireshark 抓了包??梢钥吹揭淮尾樵?,做了這么多次的交互(包含了會話初始化)。這里可以看到 #177 的交互返回查詢的結(jié)果:Tom 2020-05-14 08:00:00,與 DB 中的數(shù)據(jù)相符。可見,返回的并不是 UTC 時間。

如何使用Java中的mysql時區(qū)

在 TCP 抓包結(jié)果中 #155 的查詢語句:

/* mysql-connector-java-8.0.20 (Revision: afc0a13cd3c5a0bf57eaa809ee0ee6df1fd5ac9b) */
SELECT @@session.auto_increment_increment AS auto_increment_increment,
    @@character_set_client       AS character_set_client,
    @@character_set_connection     AS character_set_connection,
    @@character_set_results      AS character_set_results,
    @@character_set_server       AS character_set_server,
    @@collation_server         AS collation_server,
    @@collation_connection       AS collation_connection,
    @@init_connect           AS init_connect,
    @@interactive_timeout       AS interactive_timeout,
    @@license             AS license,
    @@lower_case_table_names      AS lower_case_table_names,
    @@max_allowed_packet        AS max_allowed_packet,
    @@net_write_timeout        AS net_write_timeout,
    @@performance_schema        AS performance_schema,
    @@query_cache_size         AS query_cache_size,
    @@query_cache_type         AS query_cache_type,
    @@sql_mode             AS sql_mode,
    @@system_time_zone         AS system_time_zone,
    @@time_zone            AS time_zone,
    @@transaction_isolation      AS transaction_isolation,
    @@wait_timeout           AS wait_timeout;

如何使用Java中的mysql時區(qū)

服務(wù)端返回的 time_zone 為 BST。與本地時區(qū)的轉(zhuǎn)換,由 mysql 的 connector 自動完成。

進(jìn)階

時區(qū)自動轉(zhuǎn)換

實現(xiàn)源碼:

ResultSetImpl源碼

this.defaultTimestampValueFactory = new SqlTimestampValueFactory(pset, null, this.session.getServerSession().getServerTimeZone());@Overridepublic Timestamp getTimestamp(int columnIndex) throws SQLException {
  checkRowPos();
  checkColumnBounds(columnIndex);  return this.thisRow.getValue(columnIndex - 1, this.defaultTimestampValueFactory);
}

如何確認(rèn)服務(wù)端時區(qū)?

使用會話中的服務(wù)端時區(qū)進(jìn)行服務(wù)端時區(qū)。會話初始化時會進(jìn)行時區(qū)的確認(rèn),比如前面獲取的到BST。確認(rèn)時區(qū)的邏輯在NativeProtocol#configureTimezone()中:

public void configureTimezone() {
  #從mysql的響應(yīng)獲取 time_zone 和 system_time_zone 的設(shè)置
  String configuredTimeZoneOnServer = this.serverSession.getServerVariable("time_zone");

  if ("SYSTEM".equalsIgnoreCase(configuredTimeZoneOnServer)) {
    configuredTimeZoneOnServer = this.serverSession.getServerVariable("system_time_zone");
  }

  #從 jdbc url 參數(shù) serverTimezone 獲取時區(qū)
  String canonicalTimezone = getPropertySet().getStringProperty(PropertyKey.serverTimezone).getValue();

  if (configuredTimeZoneOnServer != null) {
    //如果 jdbc url 中未通過 serverTimezone 指定時區(qū)。則從TimeZoneMapping.properties中獲取mysql 回傳的時區(qū)縮寫對應(yīng)的標(biāo)準(zhǔn)時區(qū),比如此處的 BST => Europe/London
    //會出現(xiàn)無法映射的情況,不如 CEST 無法映射到 => Europe/Berlin,可以指定自定義的 Properties 文件進(jìn)行映射
    // user can override this with driver properties, so don't detect if that's the case
    if (canonicalTimezone == null || StringUtils.isEmptyOrWhitespaceOnly(canonicalTimezone)) {
      try {
        canonicalTimezone = TimeUtil.getCanonicalTimezone(configuredTimeZoneOnServer, getExceptionInterceptor());
      } catch (IllegalArgumentException iae) {
        throw ExceptionFactory.createException(WrongArgumentException.class, iae.getMessage(), getExceptionInterceptor());
      }
    }
  }
  
  //如果 jdbc url 中通過 serverTimezone 指定了時區(qū),則優(yōu)先使用該時區(qū)
  if (canonicalTimezone != null && canonicalTimezone.length() > 0) {
    this.serverSession.setServerTimeZone(TimeZone.getTimeZone(canonicalTimezone));

    //
    // The Calendar class has the behavior of mapping unknown timezones to 'GMT' instead of throwing an exception, so we must check for this...
    //
    if (!canonicalTimezone.equalsIgnoreCase("GMT") && this.serverSession.getServerTimeZone().getID().equals("GMT")) {
      throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("Connection.9", new Object[] { canonicalTimezone }),
          getExceptionInterceptor());
    }
  }

}

關(guān)于 serverTimezone 的官方說明

Override detection/mapping of time zone. Used when time zone from server doesn't map to Java time zone

修改一下 jdbc url,通過serverTimezone指定時區(qū)為 GMT+8:jdbc:mysql://localhost:3306/test serverTimezone=GMT%2B8&useSSL=false

再次執(zhí)行代碼:

2020-05-14T08:00

看完上述內(nèi)容,是不是對如何使用Java中的mysql時區(qū)有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享名稱:如何使用Java中的mysql時區(qū)
當(dāng)前網(wǎng)址:http://aaarwkj.com/article18/gjihgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、企業(yè)網(wǎng)站制作標(biāo)簽優(yōu)化、外貿(mào)建站、手機(jī)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
青青草原在线影视一区| 曰本真人性做爰视频免费| 亚洲精品国产精品粉嫩av| 白白色发布青青在线视频观看| 国产精品果亚洲av无人区一区| 国产成人av网站在线观看| 午夜性生活免费在线观看| 日本人妻免费在线观看| 十八禁无遮挡污污污网站| 东京热一精品无码av| 日韩毛片免费看美日韩毛片| 国产真实老熟女无套内| 亚洲精品视频久久偷拍| 91口爆吞精国产对白| 久久人妻久久人妻久久| 亚洲国产视频不卡一区| 大香蕉欧美日韩在线视频| 最新日本欧美一区二区| 天天操夜夜夜夜夜操| 国产超碰久久久久久精品| 欧美日韩视频在线第一页| 欧美老熟妇子乱视频在线| 中文字幕人妻久久一区| 亚洲午夜精品美女写真| 丰满人妻少妇一区二区| 九九九热精品视频在线观看| 熟女精品国产一区二区三区| 伊人亚洲一区二区三区| 粉嫩一区二区三区精品视频| 激情欧美精品桃桃激情| 国产欧美又粗又猛又爽老 | 亚洲精品一区二区影院| 国产一区二区传媒视频| 国产污视频网站在线观看| 亚洲免费三级黄色片| 亚洲欧美中文日韩一区| 精品久久一区麻豆香蕉| 欧美黄色一级在线免费观看| 日韩有码中文字幕av| 色六月婷婷六月久久六月| 久草免费人妻视频在线|