因为换新的电脑设备,为其安装一些开发需要的应用及环境,下载了新版的Mysql8.0.13,在Eclipse中测试连接时遇到一些新的问题,遂记录。

1. Mysql 5.*  版本JDBC连接

  a. 常规导入 5.* jar 包

  

  b. 编写测试程序

package wqz.mysql.test;

import java.sql.DriverManager;

public class Test {
private static String url = "jdbc:mysql://localhost:3306/你的库名";//数据库服务地址
private static String driver = "com.mysql.jdbc.Driver";//驱动路径
private static String username = "root";
private static String passworld = "你的密码"; public static void main(String[] args) throws Exception {
Class.forName(driver).newInstance(); //如果能连接成功,则打印连接
System.out.println(DriverManager.getConnection(url, username, passworld)); }
}

  c. 运行程序

  如果数据库是Mysql 5.* 版本 ,在没有出现输入错误的连接信息情况下,会成功输出数据库连接对象。


2. 测试 Mysql 8.* 的连接

使用和上述驱动和程序 连接 Mysql 8.*,没有输出连接对象,异常信息如下 :

Tue Oct 30 10:20:06 CST 2018 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" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2330)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at wqz.mysql.test.Test.main(Test.java:14)
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2997)
at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1865)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1228)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
... 13 more

  1. 首先看第一个信息:...WARN: Establishing SSL connection without ...

  在较高版本的 Mysql 中,引入了 SSL 安全认证,需要拥有一定的安全证书去连接数据库,而我们没有证书,所以出现此警告。

  解决方式:在原数据库 url 后加上禁用 SSL 的信息

//private static String url = "jdbc:mysql://localhost:3306/你的库名";//数据库服务地址
private static String url = "jdbc:mysql://localhost:3306/你的库名?useSSL=false";//数据库服务地址

  运行程序:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2330)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at wqz.mysql.test.Test.main(Test.java:14)
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2997)
at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1865)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1228)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
... 13 more

  可见,上述警告已经不再出现,但仍无法连接成功。

  2. 解决驱动问题 

  在安装Mysql过程中会提示安装Connector/j,其中包含适应Mysql版本的JDBC连接驱动

  将其导入到项目中(如果没有此驱动,可去mysql官网下载),并删除之前的驱动:

  对比之前的驱动,由5.*  变成了  8.*,此时的程序没有变化:

package wqz.mysql.test;

import java.sql.DriverManager;

public class Test {
private static String url = "jdbc:mysql://localhost:3306/你的库?useSSL=false";
private static String driver = "com.mysql.jdbc.Driver";
private static String username = "root";
private static String passworld = "你的密码"; public static void main(String[] args) throws Exception {
Class.forName(driver).newInstance();
//如果能连接成功,则打印连接
System.out.println(DriverManager.getConnection(url, username, passworld)); }
}

  运行程序:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
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:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at wqz.mysql.test.Test.main(Test.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2234)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2258)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
... 6 more

  程序仍然运行失败,根据异常信息(主要是上面红色加大部分信息)

  解决问题:

  1. 解决驱动类路径问题

  之前版本的驱动类的 全限定名为 com.mysql.jdbc.Driver, 较新版本的驱动类的包结构发生了改变,全限定名变为 com.mysql.cj.jdbc.Driver , 更改即可

//com.mysql.jdbc.Driver
com.mysql.cj.jdbc.Driver

  2. 解决时区问题 ...The server time zone value '???ú±ê×??±??'...

  解决此问题只需要在原 url 后附加 serverTimezone=UTC

//private static String url = "jdbc:mysql://localhost:3306/你的库?useSSL=false";

private static String url = "jdbc:mysql://localhost:3306/你的库?useSSL=false&serverTimezone=UTC";

  此时的程序变为:

package wqz.mysql.test;

import java.sql.DriverManager;

public class Test {
private static String url = "jdbc:mysql://localhost:3306/你的库?useSSL=false&serverTimezone=UTC";
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String username = "root";
private static String passworld = "你的密码"; public static void main(String[] args) throws Exception {
Class.forName(driver).newInstance();
//如果能连接成功,则打印连接
System.out.println(DriverManager.getConnection(url, username, passworld)); }
}

  运行程序:成功输出连接对象


本文结束。

JDBC - Mysql 8.0.13 连接测试的更多相关文章

  1. Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://127.0.0.1:3306/test'

    原来的配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  2. MySQL 8.0.13安装修改密码的一个问题,记录一下。

    https://blog.csdn.net/qq_37350706/article/details/81707862 关于安装MySQL 8.0.13,本人就不多说了,上面这个链接讲的非常详细 请参考 ...

  3. Mysql 8.0 新特性测试

    Mysql 8.0 新特性测试 Role MySQL8.0版本添加了role特性,role是一种逻辑概念是权限的集合,可以将一个或以上的权限赋予给role,再将role赋给user.Oracle,Po ...

  4. mysql 8.0.13开启远程连接 配置方式

    1:linux登录mysql [root@localhost mysql]# mysql -u root -p Enter password: Welcome to the MySQL monitor ...

  5. JDBC_MySQL8.0.13_连接测试

    前言 手贱把MySQL升级到了8.0.13,在IntelliJ IDEA中测试连接不上.因此记录一下,供个人以后参考. 系统环境 win10x64 jkd11 IDEA MySQL 8.10.13 C ...

  6. 1、MySql的安装和连接测试并给root用户赋密码

    一.mysql数据库的安装 Windows下MySQL的配置 以 MySQL 5.1 免安装版为例, 下载 mysql-noinstall-5.1.69-win32.zip ( 官方下载页: http ...

  7. mysql 8.0 Druid连接时调用getServerCharset报空指针异常解决方法

    类似错误信息如下: 16:52:01.163 [Druid-ConnectionPool-Create-1641320886] ERROR com.alibaba.druid.pool.DruidDa ...

  8. MySQL 8.0.13(Windows压缩版本)下载安装教程

    MySQL下载安装教程 1.首先在百度上搜索mysql 2.点击链接进去,找到对应的路径 3.进去之后,可以看到版本是8.0.13,以及最下面有个Download按钮,点击下载 4.之后会跳转到开始下 ...

  9. 3-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案数据篇(安装配置数据库,使用Navicat for MySQL和手机APP 连接测试)

    2-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案数据篇(数据库简单说明) https://www.mysql.com/ 咱用安装版的 我把自己下载的放在了这里 现在 ...

随机推荐

  1. 几种流行Webservice框架

    一. 几个比较流行的Webservice框架: Apache Axis1.Apache Axis2.Codehaus XFire.Apache CXF.Apache Wink.Jboss  RESTE ...

  2. (整理)EF分页的实现

    最近做一个小功能,需要数据分页,因为小框架使用的是EF,因此查询了一下EF的分页. EF分页主要用到了skip和take两个方法: GetListBy(lamda xxxxx).skip(PageSi ...

  3. vi 常用 文本编辑 技巧

    归纳常用的Vi/Vim 文本编辑技巧,便于以后查阅. 一.把空格替换为换行 :% s/ /\r/g 二.把空行删除 :g/^$/d 三.vim以16进制打开和编辑文件 先用vim以二进制格式打开需要编 ...

  4. javac编译错误: 编码UTF8/GBK的不可映射字符

    转自:https://blog.csdn.net/leytton/article/details/52740171 Linux下为UTF-8编码,javac编译gbk编码的java文件时,容易出现“错 ...

  5. form转化json

    ;(function($){ /** * 依赖jquery-1.4.2 * 依赖jquery.json-2.2,参考http://code.google.com/p/jquery-json/ * 用于 ...

  6. CUDA compiler driver nvcc 散点 part 1

    ▶ 参考[https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html] ▶ nvcc 预定义的宏 __NVCC__ // 编译 ...

  7. 收藏 —— KVM网络虚拟化

    http://www.cnblogs.com/CloudMan6/p/5240770.html http://www.cnblogs.com/CloudMan6/p/5289590.html

  8. <记录> Razor引擎&视图布局

    标识符 @ Razor支持HTML和C#代码混编,意味着可以在HTML中随意输出变量 基本使用方法 直接@后面跟变量(当遇到 HTML标签 .空格.换行符等特殊符号时 便认为@之后到特殊符号前为变量名 ...

  9. html:class名命名规范

    1 前端开发命名规范 1.1 为什么要制定CSS命名规范 统一的命名规范,便于多人开发维护时代码统一,减少项目沟通和交接的成本,增加代码的语义化. 1.2 CSS命名规则 样式类名全部用小写,首字符必 ...

  10. Gitlab安装与备份恢复

    GitHub是2008年由Ruby on Rails编写而成,与业界闻名的Github类似;但要将代码上传到GitHub上面,而且将项目设为私有还要收费.GitLab是一个用于仓库管理系统的开源项目, ...