英文:
Hibernate + MySQL: Could not open connection
问题
我正在尝试在JAVA中使用数据库。
因此,我已经下载了MySQL,在我的root用户上设置了密码,并创建了一个名为JH的模式(schema)。
数据库正在本地主机的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>
我使用*mysqlshow*命令检查了数据库JH是否在3306端口上运行。
现在,当我尝试使用以下代码连接到数据库时:
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
beginTransaction方法抛出了*GenericJDBCException: Could not open connection*(无法打开连接):
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (usi
ng password: NO)
让我有些困惑的是,错误消息中写着:(using password: NO)(使用密码:否),
因为我在安装MySQL后为root用户设置了密码,并在配置文件中指定了密码。
我已经花了几个小时试图解决这个问题。
Hibernate版本是4.2.3Final,mysql-connector-java版本是8.0.20。
英文:
I am trying to work with Databases in JAVA.
Therefore I've downloaded MySQL, set a password for my root user and created a schema JH.
The database is running on localhost port 3306.
Now I want to persist and access data, using hibernate.
My hibernate.cfg.xml looks like this:
<?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>
I checked, if the databse JH was running on port 3306 with mysqlshow.
Now when i try to establish a connection to the database using:
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
The beginTransaction method throws an GenericJDBCException: Could not open connection:
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (usi
ng password: NO)
What confuses me a little is, that the message says: (using password: NO),
as I've set a password for root after installing MySQL and as I specified it in the configuration file.
I've been trying to solve this for a couple hours now.
Hibernate is 4.2.3Final and the mysql-connector-java is 8.0.20.
答案1
得分: 1
将 "conenction.password" 修改为 "connection.password" 在你的 hibernate.cfg.xml 文件中。
英文:
Change "conenction.password" to "connection.password" in your hibernate.cfg.xml
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论