jdbc連接數(shù)據(jù)庫(kù) The server time zone value '?й???????' is unrecognized or represents more... 錯(cuò)誤

jdbc連接數(shù)據(jù)庫(kù) The server time zone value '?й???????' is unrecognized or represents more... 錯(cuò)誤

問(wèn)題描述

連接數(shù)據(jù)庫(kù)代碼示例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
static String host = "localhost";
static int port = 3306;
static String database = "demo";
static String encoding = "UTF-8";
static String loginName = "root";
static String password = "root";
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnction() throws SQLException {
String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s", host, port, database, encoding);
return DriverManager.getConnection(url, loginName, password);
}
public static void main(String[] args) throws SQLException {
System.out.println(getConnction());
}

}

運(yùn)行main方法卻出現(xiàn)異常

Exception in thread "main" 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.

解決方法

根據(jù)異常提示可知需要添加對(duì)應(yīng)的時(shí)區(qū),修改url即可

String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s&serverTimezone=UTC", host, port, database, encoding);

完整代碼

package cn.niriqiang.tmail.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
static String host = "localhost";
static int port = 3306;
static String database = "demo";
static String encoding = "UTF-8";
static String loginName = "root";
static String password = "root";
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnction() throws SQLException {
String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s&serverTimezone=UTC", host, port, database, encoding);
return DriverManager.getConnection(url, loginName, password);
}
public static void main(String[] args) throws SQLException {
System.out.println(getConnction());
}
}

運(yùn)行main方法成功

com.mysql.cj.jdbc.ConnectionImpl@1e0f4f

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,687評(píng)論 18 399
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,011評(píng)論 0 11
  • 第一期跟閣閣老師學(xué)萌寵,第二期又跟著畫Q版人物,26號(hào)開始。今天是公開課,先熱個(gè)身。 先上成品圖。 下面是過(guò)程圖。...
    陌上花開一水間閱讀 588評(píng)論 17 8
  • 豆豆這樣的學(xué)琴狀態(tài)持續(xù)一周之后,某天在他和我共同心煩氣躁之后,我打了他的手板,打過(guò)之后認(rèn)真地找他談。他認(rèn)為我倆的不...
    暖兒_8833閱讀 264評(píng)論 0 0
  • 在上一周學(xué)到了“成本”這個(gè)經(jīng)濟(jì)學(xué)概念,今天主要談?wù)劚局艿膬纱笾R(shí)收獲,主要有以下兩個(gè)關(guān)鍵詞組成: 1、第一性原理 ...
    三木鮮森閱讀 1,025評(píng)論 0 50

友情鏈接更多精彩內(nèi)容