1

I'm trying to learn how to use Java hibernate with mysql, and I'm having troubles running the example I've downloaded. I'm getting the following error:

Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection
    at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
    at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
    ....
    at org.hibernate.persister.entity.BasicEntityPersister.insert...

**Caused by: java.sql.SQLException: Syntax error or access violation message from        server: "Access denied for user 'web'@'localhost' to database 'hibernatetutorial'"**
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
    ...
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnectio... 

My configuration file includes the following code:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property>
      <property name="hibernate.connection.username">web</property>
      <property name="hibernate.connection.password">web</property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

and the main function (which was downloaded from: http://www.roseindia.net/hibernate/runninge-xample.shtml) includes the following code:

public static void main(String[] args) {
    Session session = null;

    try{
        // This step will read hibernate.cfg.xml and prepare hibernate for use
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
         session =sessionFactory.openSession();
            //Create new instance of Contact and set values in it by reading them from form object
            System.out.println("Inserting Record");
            Contact contact = new Contact();
            contact.setId(6);
            contact.setFirstName("Deepak");
            contact.setLastName("Kumar");
            contact.setEmail("[email protected]");
            session.save(contact);
            System.out.println("Done");
    }catch(Exception e){
        System.out.println(e.getMessage());
    }finally{
        // Actual contact insertion will happen at this step
        session.flush();
        session.close();

        }

I've created the new "web" user with all the required permissions using:

CREATE USER 'web'@'localhost' IDENTIFIED BY 'web';

GRANT ALL PRIVILEGES ON hibernatetutorial.* TO web@localhost IDENTIFIED BY 'web';

And I've created the "hibernatetutorial" database. Any idea what I'm doing wrong? I don't even know how to debug this.

Thanks, Li

1 Answer 1

1

ON mydb.* TO web@localhost

shouldnt it be ON hibernatetutorial.* TO web@localhost

Sign up to request clarification or add additional context in comments.

1 Comment

right, sorry. Though after changing it the error still occurs (I tried with the "root" user first and got the same error, then I changed the user to "web" just to be certain that it's not a permission issue, but used the wrong syntax :( ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.