当前位置:网站首页>【JDBC】报错Exception in thread “main”com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communica
【JDBC】报错Exception in thread “main”com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communica
2022-07-22 19:43:00 【懒羊羊.java】
SQL写的没问题,JDBC的jar包也正常导入了,运行出现如下报错是因为数据库版本与jar包版本不一致

之前用的是5.1.47版本的jar包,而数据库却是MySQL8.0.11版本增差太大了导致无法驱动连接
导入新版本的jar包后添加到库中后就可以正常连接数据库了

当成功连接数据库后有的人可能出现这样的警告
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.
翻译:正在加载类com.mysql.jdbc。驱动程序'。这是不推荐的。新的驱动程序类是com.mysql.cj.jdbc.驱动程序。驱动程序通过SPI自动注册,通常不需要手动加载驱动程序类。

别急!我教你这样解决
首先这是一句警告,对程序运行没有任何影响 可以看到退出代码是0,这就说明程序成功执行了,但有些小伙伴有强迫症不想每次运行看到怎么办呢?
那直接按提示更新一下驱动就可以
Class.forName("com.mysql.cj.jdbc.Driver");连接模板:
public static void main(String[] args) throws SQLException, ClassNotFoundException {
//1.注册驱动
Driver driver = new Driver();
//2.得到链接
String url = "jdbc:mysql://localhost:3306/pyq";
//将用户名和密码放入到Properties对象
Properties properties=new Properties();
properties.setProperty("user","root");
properties.setProperty("password","284650"); //自己设置的密码
Connection connect = driver.connect(url, properties);
//3.执行SQL
String sql="insert into actor values(null,'彭于晏','男','2000-5-20','220')";
Statement statement = connect.createStatement();
int rows = statement.executeUpdate(sql);
//4.关闭连接
statement.close();
connect.close();
}边栏推荐
- session、cookie、token 详解
- 一根网线怎么连接两台电脑传文件 一根网线连接两台电脑的方法
- 微软我们有多像上传不了照片怎么回事?TwinsOrNot照片传不上去的解决方法
- What does DNS mean? What is the function of DNS
- How to quickly shut down the computer shut down command sharing
- 桌面图标不能拖动怎么回事 桌面文件图标无法拖动现象的解决方法介绍
- 电脑屏幕变大了怎么还原 电脑屏幕显示缩放教程
- 如何关闭电脑乱七八糟的弹窗 电脑禁止一切弹窗广告设置教程
- ipv4无internet访问权限怎么办?ipv4无internet访问权限解决方法(图文详解)
- 电脑桌面卡住了怎么办?电脑死机桌面卡死现象的解决办法介绍
猜你喜欢

LUR caching algorithm

Realize the national standard gb28181 streaming media service solution

The ability to detect movement in vivo and build a safe and reliable payment level "face brushing" experience

CV目标检测模型小抄(1)

小黑leetcode之旅:100 相同的树

Realize OPC UA publish/subscribe single send

MySQL --- 子查询 - 子查询概念、规范、分类

Gb28181 summary of common problems in the use and secondary development of livegbs streaming media service

Kotlin学习快速入门(8)—— 委托

小黑啃leetcode:589. N 叉树的前序遍历
随机推荐
记事本文件太大打不开怎么办?TXT文件太大无法打开现象的解决办法介绍
局域网SDN技术硬核内幕 - 15 三 从物到人 园区用户漫游的MPLS实现
小黑leetcode之旅:590. N 叉树的后序遍历
PHP prevents or detects repeated post submissions when the page is refreshed
FileInputFormat.setInputPaths多路径读取规则
What is the difference between GPU and CPU? Introduction to the meaning of GPU in different computers
js中的迭代器与生成器(详解)
Face algorithms
How to solve the problem that telnet is not an internal or external command? Telnet is not an internal or external command solution
What is the difference between 32-bit and 64 bit computers
dns是什么意思 dns作用是什么介绍
动作活体检测能力,构建安全可靠的支付级“刷脸”体验
Pikachu shooting range SQL injection search injection clearance steps
电脑cmd重置网络设置 重置网络的cmd命令
电脑下载的软件不在桌面显示怎么办 解决安装后的软件不在桌面问题
[matlab project practice] analysis of spatial and temporal characteristics of drought in a region based on SPI index
【FAQ】应用内支付服务无法拉起支付页面常见原因分析和解决方法
局域网SDN技术硬核内幕 - 02 前传 多核技术为摩尔定律续一秒
电脑屏幕变大了怎么还原 电脑屏幕显示缩放教程
Demo19-(待更新)