解决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...");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值