Search code examples
javamysqlhibernatesqlexception

Hibernate + MySQL: Could not open connection


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.


Solution

  • Change "conenction.password" to "connection.password" in your hibernate.cfg.xml