版本问题
如果运行时控制台报错
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
报这个错误很大的可能是数据库版本和jdbc-jar包版本不匹配,可以检查一下
如果运行时控制台报以下错误
Thu May 06 10:27:49 CST 2021 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
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.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at com.day1.jdbc_bacics.JdbcDemo1.main(JdbcDemo1.java:14)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 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.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
... 6 more
很大可能是url写错了
jdbc5.x url写法:jdbc:mysql://localhost:3306/数据库名称
jdbc8.x url写法 :jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
- useUnicode=true&characterEncoding=utf-8 指定字符编码,常用字符编码有:
- gbk
- utf-8
- useSSL=false 指明是否进行SSL连接
- serverTimezone=“指定时区” 常用时区介绍:
- Asia/Shanghai 上海时间
- GMT%2BB 北京时间东八区
- UTC 全球标准时间比北京时间晚8个小时
其它错误
如果你看到以下错误信息,那么账号有可能写错了
Exception in thread "main" java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
如果你看到以下错误信息,那么密码可能写错了
Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
如果报错信息如下,那么请检查url
Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
扩展小知识
jdbc 5.x版本后注册驱动可以省略
注册驱动版本问题
jdbc 5.x注册驱动 : com.mysql.jdbc.Driver
jdbc 8.x注册驱动:com.mysql.cj.jdbc.Driver