Hibernate + MySQL: 无法打开连接

huangapple go评论78阅读模式
英文:

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:

&lt;?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?&gt;
&lt;!DOCTYPE hibernate-configuration PUBLIC &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot; &quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;
&lt;hibernate-configuration&gt;
	&lt;session-factory&gt;
		&lt;property name=&quot;hibernate.current_session_context_class&quot;&gt;
			thread
		&lt;/property&gt;
		&lt;property name=&quot;connection.url&quot;&gt;
			jdbc:mysql://localhost:3306/JH
		&lt;/property&gt;
		&lt;property name=&quot;connection.driver_class&quot;&gt;
			com.mysql.jdbc.Driver
		&lt;/property&gt;
		&lt;property name=&quot;connection.username&quot;&gt;
			root
		&lt;/property&gt;
		&lt;property name=&quot;conenction.password&quot;&gt;
			Password
		&lt;/property&gt; 
		&lt;property name=&quot;dialect&quot;&gt;
			org.hibernate.dialect.MySQL5Dialect
		&lt;/property&gt;
	&lt;mapping resource=&quot;Movie.hbm.xml&quot; /&gt;
	&lt;/session-factory&gt;
&lt;/hibernate-configuration&gt;

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 &#39;root&#39;@&#39;localhost&#39; (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

huangapple
  • 本文由 发表于 2020年5月5日 06:48:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61602925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定