使用DataSourc获取数据库连接报java.sql.SQLException: Access denied for user ‘‘@‘localhost‘ (using password: NO)

错误信息

java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)

原因

原因就是我们没有传入或者没有配置数据库的usernamepassword,我自己开始写springboot使用JDBC获取连接对象时,我出现这个错误的原因是

在这里插入图片描述
我居然使用的是data-username,data-password来连接数据库,怪自己学艺不精,别笑啊,对,就说你呢。严肃点。后面改成下面的图就好了。
在这里插入图片描述
我的测试代码:

@SpringBootTest
class SpringBootDataJdbcApplicationTests {
    
    

    @Autowired
    DataSource dataSource;

    @Test
    void contextLoads() throws SQLException {
    
    
    	// com.zaxxer.hikari.HikariDataSource
        System.out.println(dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        // 关闭连接
        connection.close();
    }

}

猜你喜欢

转载自blog.csdn.net/qq_47768542/article/details/110957234