解决Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: ~link failure

看狂神的Mysql时,jdbc连接数据库时候,报错:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure.

1、数据库连接不上应该和连接数据库时的输入参数相关DriverManager.getConnection(url,username,password),尝试检查url、用户名:username、密码:password三个参数。

//报错的程序,错误的位置在第3部分
public static void main(String[] args) throws ClassNotFoundException, SQLException {
	
	//1、加载驱动
	Class.forName("com.mysql.jdbc.Driver");//固定写法,加载驱动
	
	//2、连接数据库,用户信息和url
	//useUnicode=true&characterEncoding=utf8&&useSSL=true 支持中文编码,设定字符集,使用安全连接
	String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
	String username = "root";
	String password = "123456";

	//3、连接成功,数据库对象 Connection代表数据库 
	Connection connection = DriverManager.getConnection(url,username,password);
	
	//4、执行SQL的对象 执行sql的对象
	Statement statement = connection.createStatement();
	
	//5、执行SQL的对象 去 执行SQL,可能存在结果,查看返回结果
	String sql = "SELECT * FROM users";
	ResultSet resultSet = statement.executeQuery(sql);//返回的结果集,结果集中封装了全部查询出来的结果
	while(resultSet.next()){//如果存在下一条数据,我们就输出一下
		System.out.println("id="+resultSet.getObject("id"));
	}
	
	//6、释放连接
	resultSet.close();
	statement.close();
	connection.close();
	System.out.println("end...");
}               

 2、这里将url中问号?后边的第三个参数,也就是安全连接的参数设置为useSSL= false或者是直接去掉安全连接参数,即将url改为:

//设置安全连接参数useSSL=false
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false";

//去掉该参数,但是会存在警告
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8";


MySQL在高版本需要指明是否进行SSL连接:(1)true 需要连接;(2)false 不需要连接

3、通过jdbc成功连接数据库并查询代码

package com.kuang.lesson01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

// 第一个jdbc程序
public class jdbcFirstDemo {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		//1、加载驱动
		Class.forName("com.mysql.jdbc.Driver");//固定写法,加载驱动
		
		//2、连接数据库,用户信息和url
		//useUnicode=true&characterEncoding=utf8&&useSSL=false 支持中文编码,设定字符集,使用安全连接
		String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false";
		String username = "root";
		String password = "123456";
		//3、连接成功,数据库对象 Connection代表数据库
		Connection connection = DriverManager.getConnection(url,username,password);
		
		//4、执行SQL的对象 执行sql的对象
		Statement statement = connection.createStatement();
		
		//5、执行SQL的对象 去 执行SQL,可能存在结果,查看返回结果
		String sql = "SELECT * FROM users";
		ResultSet resultSet = statement.executeQuery(sql);//返回的结果集,结果集中封装了全部查询出来的结果
		while(resultSet.next()){//如果存在下一条数据,我们就输出一下
			System.out.println("id="+resultSet.getObject("id"));
		}
		
		//6、释放连接
		resultSet.close();
		statement.close();
		connection.close();
		System.out.println("end...");
	}
}

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure是一个嵌套异常,表示与MySQL服务器的通信链接失败。 这个异常通常是由于以下原因之一导致的: 1. 网络连接问题:可能是由于网络不稳定或防火墙设置问题导致的通信链接失败。 2. MySQL服务器未运行:确保MySQL服务器正在运行,并且可以通过给定的主机名或IP地址进行访问。 3. 错误的连接配置:请检查连接配置,包括主机名、端口、用户名和密码是否正确。 4. 连接超时:如果连接超时设置过小,可能会导致链接失败。可以尝试增加连接超时时间来解决问题。 解决这个问题的方法包括: 1. 检查网络连接:确保你的网络连接正常,可以访问到MySQL服务器。 2. 检查MySQL服务器状态:确认MySQL服务器正在运行,并且可以通过给定的主机名或IP地址进行访问。 3. 检查连接配置:确保连接配置中的主机名、端口、用户名和密码等信息正确无误。 4. 增加连接超时时间:如果连接超时设置过小,可以尝试增加连接超时时间。 综上所述,nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure表示与MySQL服务器的通信链接失败,解决方法包括检查网络连接、确认MySQL服务器状态、检查连接配置和增加连接超时时间等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [数据库 异常:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException](https://blog.csdn.net/Greahow/article/details/99336892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [java解决nested exception is java.lang.OutOfMemoryError Java heap space](https://download.csdn.net/download/lj_70596/14122361)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值