Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use JDBC to connect to MySQL 8.x

Shulou Source: shulou.com Published: 2022-05-31 15:56:01 10月03日 Update

This article is about how to use JDBC to connect to MySQL 8.x. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

MySQL 8.x new version JDBC connection method, old version, MySQL Connector/J 5.x version connection method, MySQL 8.x new version, MySQL Connector/J 6.x connection method, the new version of mysql driver url must be set time zone, that is, serverTimezone=UTC, otherwise an error will be reported.

1. Connection method for old version, MySQL Connector/J version 5.x:

Url = jdbc:mysql://localhost:3306/thrcloud_db01?useUnicode=true&characterEncoding=utf8driver-class-name = com.mysql.jdbc.Driver

2. The new version of MySQL 8.x, the connection method after MySQL Connector/J 6.x:

Url = jdbc:mysql://localhost:3306/thrcloud_db01?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=falsedriver-class-name = com.mysql.cj.jdbc.Driver

Note: the url of the new mysql driver must set the time zone, that is, serverTimezone=UTC, otherwise the following error will be reported:

Java.sql.SQLException: the host in the The server time zone value 'list is the most expensive 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. At com.mysql.cj.jdbc.exceptions.SQLError.createSQLException (SQLError.java:129) ~ [mysql-connector-java-8.0.14.jar:8.0.14] at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException (SQLError.java:97) ~ [mysql-connector-java-8.0.14.jar:8.0.14] at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException (SQLError.java:89) ~ [mysql-connector-java-8 .0.14.jar: 8.0.14] at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException (SQLError.java:63) ~ [mysql-connector-java-8.0.14.jar:8.0.14] at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException (SQLError.java:73) ~ [mysql-connector-java-8.0.14.jar:8.0.14] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException ( SQLExceptionsMapping.java:76) ~ [mysql-connector-java-8.0.14.jar:8.0.14] at com.mysql.cj.jdbc.ConnectionImpl.createNewIO (ConnectionImpl.java:835) ~ [mysql-connector-java-8.0.14.jar:8.0.14] at com.mysql.cj.jdbc.ConnectionImpl. (ConnectionImpl.java:455) ~ [mysql-connector-java-8.0.14.jar:8.0.14] at com.mysql. Cj.jdbc.ConnectionImpl.getInstance (ConnectionImpl.java:240) ~ [mysql-connector-java-8.0.14.jar:8.0.14] at com.mysql.cj.jdbc.NonRegisteringDriver.connect (NonRegisteringDriver.java:199) ~ [mysql-connector-java-8.0.14.jar:8.0.14] at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection (DruidAbstractDataSource.java:1464) ~ [druid-1.0.31.jar:1.0.31] At com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection (DruidAbstractDataSource.java:1529) ~ [druid-1.0.31.jar:1.0.31] at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run (DruidDataSource.java:2109) ~ [druid-1.0.31.jar:1.0.31] Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value'in the last 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. At sun.reflect.GeneratedConstructorAccessor29.newInstance (Unknown Source) ~ [na:na] Thank you for reading! This is the end of the article on "how to use JDBC to connect to MySQL 8.x". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Mode version content time zone more articles posts drivers good practical articles see knowledge errors references help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Docker MySQL Microsoft