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("deepak_38@yahoo.com");
            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

4

1 回答 1

1

ON mydb.* TO web@localhost

不应该 ON hibernatetutorial.* TO web@localhost

于 2012-04-13T18:16:24.213 回答