Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JDBC to connect to MySQL 8.x

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report