最近在做一些java web整合时使用的最新版Mysql8.0.3,发现Mysql连接中的几个问题,总结如下:

package db;//自定义包名
import java.sql.*; public class test1 { public static void main(String[] args) {
// TODO Auto-generated method stub
String user="root";
String password="123456";
String url="jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC";//mydb为Mysql数据库中创建的数据库实例名
String driver="com.mysql.cj.jdbc.Driver"; String tableName="studinfo";//studinfo为数据库mydb中的表名
String sqlstr;
Connection con=null;
Statement stmt=null;
ResultSet rs=null; try
{
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
stmt=con.createStatement(); sqlstr="insert into "+tableName+ " value('1111','honey',21)";//into的后面和value前面一定要添加一个空格;value后面与左括号之间有无空格无所谓。
stmt.executeUpdate(sqlstr); sqlstr="select * from "+ tableName;
rs=stmt.executeQuery(sqlstr); ResultSetMetaData rsmd=rs.getMetaData();
int j=0;
j=rsmd.getColumnCount();
for(int k=0;k<j;k++)
{
System.out.print(rsmd.getColumnName(k+1));
System.out.print("\t");
} System.out.println(); while(rs.next())
{
for(int i=0;i<j;i++)
{
System.out.print(rs.getString(i+1));
System.out.print("\t");
}
System.out.println();
}
}
catch(ClassNotFoundException e1)
{
System.out.print("数据库驱动不存在!");
System.out.print(e1.toString());
}
catch(SQLException e2)
{
System.out.print("数据库存在异常!");
System.out.print(e2.toString());
}
finally
{
try
{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
catch(SQLException e)
{
System.out.print(e.toString());
}
}
}
}

  常见错误提示1:

  以上配置中,url中如果driver没有添加cj,则会在连接的时候出现以下错误提示:

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.

  解决办法:根据提示,很显然这种driver配置方式在此版本中已经被废弃,因此需要将driverClass配置为:com.mysql.cj.jdbc.Driver。

  常见错误提示2:

  以上配置中,url中如果没有设置useSSL=false,则会在连接的时候出现以下错误提示:

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.

  解决办法:在连接字符串中添加?useSSL=false

  常见错误提示3:

  以上配置中,url中如果没有设置serverTimezone=UTC,则会在连接的时候出现以下错误提示:

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.

  解决办法:在连接字符串中添加&amp;serverTimezone=UTC

  常见错误提示4

  错误提示:对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。

  解决办法:在 xml 中 &符号是作为实体字符形式存在的。故需要在连接字符串中的将ServerTime前面的&符号修改为&amp;,参见上面的代码。

初学者在Mysql8.0连接时的几个常见基本问题的更多相关文章

  1. 错误档案2:MySQL8.0连接C3P0的问题

    >>>跳过BB,空降正文<<< 目录 前言 问题出现 解决方法 结论 前言 大家好呀,我是 白墨,一个热爱学习与划水的矛盾体. 前两天在使用C3P0连接池时遇到问题 ...

  2. maven 项目连接mysql8.0版本时的注意事项

    MySQL 8.0 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 以前的maven项目,要注意依赖的注入 查看pom. ...

  3. Navicat连接MySQL8.0版本时 建议升级连接客户端这个提示怎么办

    开始->mysql 8.0 command line client ->执行下面的命令//开启mysql服务mysql.server start//进入mysqlmysql -u root ...

  4. mysql8.0安装时,Unable to connect to any of the specified MySQL hosts

    https://blog.csdn.net/u014776759/article/details/88422967

  5. VMware workstation16 中Centos7下MySQL8.0安装过程+Navicat远程连接

    1.MySQL yum源安装 2.安装后,首次登录mysql以及密码配置3.远程登录问题(Navicat15为例) 一.CentOS7+MySQL8.0,yum源安装1.安装mysql前应卸载原有my ...

  6. win10安装两个不同版本的mysql(mysql5.7和mysql-8.0.19)

    win10中安装mysql5.7后,安装mysql-8.0.19 在D:\mysql-8.0.19-winx64目录下创建一个my.ini文件 [mysqld] # 设置3307端口 port # 设 ...

  7. mysql8.0新增用户及密码加密规则修改

    MySQL8.0已经发布GA版,当前最新GA版本为8.0.12.虽然相对于之前版本,MySQL8.0没有加入新元素,但是,经过代码重构,MySQL8.0的优化器更加强大,同时也有一些新特性,如支持索引 ...

  8. 远程连接mysql8.0,Error No.2058 Plugin caching_sha2_password could not be loaded

    通过本地去连接远程的mysql时报错,原因时mysql8.0的加密方法变了. mysql8.0默认采用caching_sha2_password的加密方式 第三方客户端基本都不支持这种加密方式,只有自 ...

  9. 登录注册页面(连接MySQL8.0.15版本)

    原文链接:https://mp.weixin.qq.com/s?__biz=MzI4Njg5MDA5NA==&mid=2247483779&idx=1&sn=e23e68e96 ...

随机推荐

  1. php扩展php-redis安装与使用

    一.redis的安装 1,安装redis版本 下载页面:https://redis.io/download 安装一个老版本3.2.11:http://download.redis.io/release ...

  2. @Schedul 中cron的命名规则

    @Schedul注解的定时任务详解 1.springboot集成schedule由于Spring Schedule包含在spring-boot-starter基础模块中了,所有不需要增加额外的依赖. ...

  3. LeetCode题解之Rotated Digits

    1.题目描述 2.代码 int rotatedDigits(int N) { ; ; i <= N; i++) { if (isGood(i)) { res++; } } return res; ...

  4. LeetCode题解之Find All Duplicates in an Array

    1.题目描述 2.问题分析 将数组中的元素 A[i] 放到 A[ A[i] - 1] 的位置.然后遍历一边数组,如果不满足 A[i] == i+1,则将A[i]添加到 结果中. 3.代码 vector ...

  5. pycharm的常用快捷键

      使用pycharm写代码时,如果有错误,一般代码右边会有红色标记.   1,写代码时忘记导入模块,可以使用快捷键 Alt + Enter 自动导入模块.() 再倒入模块之前,需要现在pycharm ...

  6. Visual Basic 6.0(VB6.0)详细安装过程

    注:大家如果没有VB6.0的安装文件,可自行百度一下下载,一般文件大小在200M左右的均为完整版的软件,可以使用. 特别提示:安装此软件的时候最好退出360杀毒软件(包括360安全卫士,电脑管家等,如 ...

  7. c#中的数据类型简介(委托)

    什么是委托? 委托是一种类型,它封装了一类方法,这些方法具有相同的方法签名(signature)和返回类型.定义听起来有点拗口,首先可以确定委托是一种数据类型,那么什么是方法签名,其实就是指方法的输入 ...

  8. Linux 中 FQDN 查询及设置

    FQDN:(Fully Qualified Domain Name)全限定域名:同时带有主机名和域名的名称 其实就是标注一个主机的完整域名.比如我的域名为 ifrom.top 那么它的邮件服务器的主机 ...

  9. ln -s 软连接介绍

    软连接(softlink)也称符号链接.linux里的软连接文件就类似于windows系统中的快捷方式.软连接文件实际上是一个特殊的文件,文件类型是I.软连接文件实际上可以理解为一个文本文件,这个文件 ...

  10. linux开机步骤

    linux开机启动步骤: 1.bios自检 2.MBR引导 3.引导系统,进入grub菜单 4.加载内核kernel 5.运行第一个进程init 6.读取/etc/inittab 读取运行级别 7.读 ...