Java SQL“语法错误或访问冲突”

时间:2012-04-13 18:08:08

标签: java mysql hibernate

我正在尝试学习如何将Java hibernate与mysql一起使用,并且运行我下载的示例时遇到了麻烦。 我收到以下错误:

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... 

我的配置文件包含以下代码:

<?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>

并且主要功能(从http://www.roseindia.net/hibernate/runninge-xample.shtml下载)包括以下代码:

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();

        }

我使用以下方法创建了具有所有必需权限的新“网络”用户:

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

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

我创建了“hibernatetutorial”数据库。 知道我做错了什么吗?我甚至不知道如何调试它。

谢谢, 李

1 个答案:

答案 0 :(得分:1)

ON mydb.* TO web@localhost

不应该 ON hibernatetutorial.* TO web@localhost