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

Hibernate 启动报错系列
(一)缺少数据库的方言配置:Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
(二)缺少数据库的用户名和密码配置:Caused by: java.sql.SQLException: Access denied for user ‘‘@‘localhost‘ (using password: NO)

一、报错问题

Caused by: java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: NO)
报错截图

二、问题背景

新建Java项目,并添加 Hibernate 框架支持,启动测试(运行默认的Main类中的main()方法),出现报错。

Main.java

import org.hibernate.HibernateException;
import org.hibernate.Metamodel;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javax.persistence.metamodel.EntityType;

import java.util.Map;

public class Main {
    private static final SessionFactory ourSessionFactory;

    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            ourSessionFactory = configuration.buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
        final Session session = getSession();
        try {
            System.out.println("querying all the managed entities...");
            final Metamodel metamodel = session.getSessionFactory().getMetamodel();
            for (EntityType<?> entityType : metamodel.getEntities()) {
                final String entityName = entityType.getName();
                final Query query = session.createQuery("from " + entityName);
                System.out.println("executing: " + query.getQueryString());
                for (Object o : query.list()) {
                    System.out.println("  " + o);
                }
            }
        } finally {
            session.close();
        }
    }
}

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 配置hibernate的基本信息 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <mapping class="com.hibernate.helloworld.TblAccountEntity"/>
        <!-- <property name="connection.username"/> -->
        <!-- <property name="connection.password"/> -->

        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->


    </session-factory>
</hibernate-configuration>

三、原因分析

缺少数据库的用户名和密码配置。

四、解决方案

在 Hibernate 配置文件 hibernate.cfg.xml 中,添加数据库的用户名和密码配置。

        <property name="connection.username">root</property>
        <property name="connection.password">******</property>

完整示例如下:
hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 配置hibernate的基本信息 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="connection.username">root</property>
        <property name="connection.password">******</property>
        <mapping class="com.hibernate.helloworld.TblAccountEntity"/>

        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->


    </session-factory>
</hibernate-configuration>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值