版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Java_Glory/article/details/90173441
IDEA连接mysql数据库时报错 java.sql.SQLException:Access denied for user ''@'localhost' (using password: YES)
对这个错误表示蛋疼,由于IDEA智能提示的原因,没有注意看提示的候选项,下意识的选中了name:
所以就报 Access denied for user ''@'localhost' (using password: YES) ,修改过访问权限还是不行,仔细核对属性名称,才发现是选的属性不对,将属性修改为username才正常:
测试访问数据库连接:
package com.mydata.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDataApplicationTests {
@Autowired
DataSource dataSource;
@Test
public void contextLoads() throws SQLException {
System.out.println("数据源class:"+dataSource.getClass());
try (Connection connection = dataSource.getConnection()) {
System.out.println("获取数据连接:"+connection);
}
}
}
OK!这样显示就说明数据库已经连接成功: