1

当我执行我的主类时,我得到这个执行无法找出问题点

错误出现在 Transaction tr = session.beginTransaction();

错误堆栈说:

ERROR: Access denied for user 'root'@'localhost' (using password: NO)
error===>org.hibernate.exception.GenericJDBCException: Could not open connection

我的主类文件:

package com.hussi.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Main {
    public static void main(String[] args) 
    {
        User user = new User();
        user.setUser_id(1);
        user.setUsername("hussi");
        user.setPassword("maria");
        SessionFactory sesionFactory = new Configuration().configure().buildSessionFactory() ;
        Session session = sesionFactory.openSession();
        try{
        Transaction tr = session.beginTransaction();
        session.save(user);
        }
        catch(Exception e)
        {
            System.out.println("error===>"+e);
        }

        finally
        {
        session.flush();
        session.close();
        }
    }

}

我的模型文件

package com.hussi.model;

public class User 
{
    int user_id;
    String username;
    String password;

    public int getUser_id() {
        return user_id;
    }
    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }


    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }


    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String toString()
    {
        return "username==>"+this.username+" : password==>"+this.password;
    }

}

我的 user.hbm.xml 文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.hussi.model.User" table="users">
         <id name="user_id" type="int" column="user_id">
            <generator class="increment" />
        </id>

        <property name="username">
            <column name="username"/>
        </property>

        <property name="password">
            <column name="password"/>
        </property>
    </class>
</hibernate-mapping>

我的休眠配置文件: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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/my_hibernate_1</property>
        <property name="connection.username">root</property>
        <property name="connecttion.password">root</property>

        <!-- Database connection settings -->
        <property name="connection.pool_size">1</property>

        <!-- MySql Dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>

        <mapping resource="user.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
4

1 回答 1

1

我相信您需要重置数据库密码。按照此链接执行相同操作:

http://dev.mysql.com/doc/refman/5.1/en/resetting-permissions.html

或者用户权限不正确。按照此设置权限:

http://dev.mysql.com/doc/refman/5.1/en/default-privileges.html

于 2013-06-30T15:57:41.907 回答