: Communications link failure

Java连接数据失败,我使用的Mysql的版本是8.0需要将URl改写成加上默认的字符集,如下图所示

package JDBC_connection;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

import org.junit.Test;

public class Connection_test {
    
    

	
	@Test
	public void testConnection1() throws SQLException {
    
    
		Driver driver=new com.mysql.jdbc.Driver();
		
		// jdbc:mysql 数据库协议
		//localhost: ip地址
		// 3306: 默认端口号
		//test:连接的数据库的名称
		
		
		String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" ;
	
		
		// 以键值对的形式存储用户名和密码
		
		Properties info=new Properties();
		
		// 将用户名和密码封装在propertise中
		info.setProperty("user", "root");
		info.setProperty("password", "123456");
		try {
    
    
			Connection connection=driver.connect(url, info);
			System.out.println(connection);
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

猜你喜欢

转载自blog.csdn.net/weixin_46351306/article/details/114460895