Java连接MySQL数据库报com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure错误

我使用的是MySQL8.0的版本,代码如下:


package mysql;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

 

public class mysql {

	// 数据库地址

	private static String dbUrl = "jdbc:mysql://localhost:3306/student?"
			+ "useSSL=true&characterEncoding=utf-8&serverTimezone=GMT";

	// 用户名

	private static String dbUserName = "root";

	// 密码

	private static String dbPassword = "123456";

	// 驱动名称

	private static String jdbcName = "com.mysql.cj.jdbc.Driver";

 

	public static void main(String[] args) {

		try {

			Class.forName(jdbcName);

			System.out.println("加载驱动成功!");

		} catch (ClassNotFoundException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			System.out.println("加载驱动失败!");

		}

 

		Connection con = null;

		try {

			// 获取数据库连接
			
			con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);

			System.out.println("获取数据库连接成功!");

			System.out.println("进行数据库操作!");

		} catch (SQLException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			System.out.println("获取数据库连接失败!");

		} finally {

			try {

				con.close();

			} catch (SQLException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();

			}

		}

 

	}

}

结果报错:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

Java连接MySQL数据库报com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure错误_第1张图片

 

解决办法,使用jdbc8.0.16版本就可以了

下载地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.16

 

 

 

 

 

 

 

你可能感兴趣的:(Java连接MySQL数据库报com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure错误)