我想在JAVA中使用数据库,因此我下载了MySQL,为我的root用户设置了密码,并创建了一个模式JH,数据库运行在localhost的3306端口。
现在我想使用hibernate来持久化和访问数据。hibernate.cfg.xml。 看起来像这样。
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.current_session_context_class">
thread
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/JH
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">
root
</property>
<property name="conenction.password">
Password
</property>
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<mapping resource="Movie.hbm.xml" />
</session-factory>
</hibernate-configuration>
我检查了一下,如果数据库JH在3306端口上运行时 mysqlshow.
现在,当我试图使用数据库建立一个连接。
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
beginTransaction方法会抛出一个 GenericJDBCException。无法打开连接:
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (usi
ng password: NO)
让我有点疑惑的是,消息中说。(使用密码:NO),因为我在安装MySQL后已经为root设置了密码,而且我在配置文件中指定了它。
我已经尝试解决这个问题几个小时了,Hibernate是4.2.3Final,mysql-connector-java是8.0.20。
将hibernate.cfg.xml中的 "conenction.password "改为 "connection.password"。