JDBC Connection Database The server time zone value ' Йʱ ' are unrecognized or represents more ... Error
Problem Description
Connection Database code example
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 ());
}
The Main method is running with an exception
Exception in thread "main" Java.sql.SQLException:The Server time zone value ' Йʱ ' is unrecognized or represents MOR E than one time zone. You are must configure either the server or JDBC driver (via the Servertimezone configuration) to use a more specifc Time zone value if your want to utilize time zone support. Solving Method depending on the exception hint, you need to add the corresponding time zone, modify the URL can
String url = String.Format ("JDBC:MYSQL://%S:%D/%S?CHARACTERENCODING=%S&SERVERTIMEZONE=UTC", host, Port, database , encoding); Complete Code
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:mys QL://%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 ());
}
} Run Main method succeeded
COM.MYSQL.CJ.JDBC.CONNECTIONIMPL@1E0F4F