因为换新的电脑设备,为其安装一些开发需要的应用及环境,下载了新版的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. cmd下PUSHD和POPD命令使用说明

    PUSHD命令保存当前目录以供 POPD 命令使用,然后改到指定的目录. PUSHD [path | ..] path 指定要成为当前目录的目录. 如果命令扩展被启用,除了一般驱动器号和路径,PUSH ...

  2. MFC 中CString 格式16进制转int 十进制

    代码:CString  v_hex ; int v_dec; v_dec = wcstol(v_hex, NULL, 16);

  3. 0003 - 基于xml的Spring Bean 的创建过程

    一.目录 前言 创建 Bean 容器 加载 Bean 定义 创建 Bean Spring Bean 创建过程中的设计模式 总结 二.前言 2.1 Spring 使用配置 ApplicationCont ...

  4. SQLServer、MySQL、Oracle如何查看所有表的条数

    SQLServer: create table #t(name varchar(255), rows bigint, reserved varchar(20), data varchar(20), i ...

  5. 将打印(printk/printf)及时写入文件的方法

    问题是这样的,在测试一个gps的app的时候,我使用脚本  “ gps_test_app  > /tmp/gps_log.txt &" 但是但是,去查看gps_log.txt的 ...

  6. centos7 设置系统默认启动的界面

    系统默认 以某种方式启动 使用systemd创建符号链接指向默认运行级别. 修改方法为:在root下 1.首先删除已经存在的符号链接rm /etc/systemd/system/default.tar ...

  7. linux 获取帮助的命令

    Linux命令详解:[7]获得命令帮助 听语音 | 浏览:4601 | 更新:2015-01-30 20:21 | 标签:linux 1 2 3 4 5 分步阅读 在维护和使用Linux系统时,常常会 ...

  8. dubbo使用简介

    ---------------------------------------------------------------------------------------------------- ...

  9. jqueryValidate

    参数详情可参见: http://www.runoob.com/jquery/jquery-plugin-validate.html 基本使用: /** 数据保存前校验 **/ $("#use ...

  10. SSH登录启用Google二次身份验证

    一般来说,使用ssh远程登录服务器,只需要输入账号和密码,显然这种方式不是很安全.为了安全着想,可以使用GoogleAuthenticator(谷歌身份验证器),以便在账号和密码之间再增加一个验证码, ...