JDBC Connection Database The server time zone value ' Й׼ʱ ' are unrecognized or represents more ... Error __ Database

Source: Internet
Author: User
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

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.