神笔马良2018 2016-06-30 04:20 采纳率: 100%
浏览 1848
已采纳

hibernate连接数据库报错

这是hibernate.cfg.xml配置文件,用户名和密码都用jdbc测试过可以连接上

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.uesrname">root</property>
        <property name="hibernate.connection.password">liangqichan</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cql1</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="com/cql/crm/domain/SysUserGroup.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

这是报错信息:
2016-6-30 11:56:43 org.hibernate.cfg.Environment
信息: Hibernate 3.5.6-Final
2016-6-30 11:56:43 org.hibernate.cfg.Environment
信息: hibernate.properties not found
2016-6-30 11:56:43 org.hibernate.cfg.Environment buildBytecodeProvider
信息: Bytecode provider name : javassist
2016-6-30 11:56:43 org.hibernate.cfg.Environment
信息: using JDK 1.4 java.sql.Timestamp handling
2016-6-30 11:56:43 org.hibernate.cfg.Configuration configure
信息: configuring from resource: /hibernate.cfg.xml
2016-6-30 11:56:43 org.hibernate.cfg.Configuration getConfigurationInputStream
信息: Configuration resource: /hibernate.cfg.xml
2016-6-30 11:56:43 org.hibernate.cfg.Configuration addResource
信息: Reading mappings from resource : com/cql/crm/domain/SysUserGroup.hbm.xml
2016-6-30 11:56:43 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
信息: Mapping class: com.cql.crm.domain.SysUserGroup -> sys_user_group
2016-6-30 11:56:43 org.hibernate.cfg.Configuration doConfigure
信息: Configured SessionFactory: null
2016-6-30 11:56:43 org.hibernate.connection.DriverManagerConnectionProvider configure
信息: Using Hibernate built-in connection pool (not for production use!)
2016-6-30 11:56:43 org.hibernate.connection.DriverManagerConnectionProvider configure
信息: Hibernate connection pool size: 20
2016-6-30 11:56:43 org.hibernate.connection.DriverManagerConnectionProvider configure
信息: autocommit mode: false
2016-6-30 11:56:43 org.hibernate.connection.DriverManagerConnectionProvider configure
信息: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/cql1
2016-6-30 11:56:43 org.hibernate.connection.DriverManagerConnectionProvider configure
信息: connection properties: {uesrname=root, password=****}
2016-6-30 11:56:43 org.hibernate.cfg.SettingsFactory buildSettings
警告: Could not obtain connection to query metadata
java.sql.SQLException: Access denied for user ''@'localhost' (using password: YES

补充:
这是我测试hibernate时的代码
package junit;

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

import com.cql.crm.domain.SysUserGroup;

public class TestHibernate {

@Test
public void testHibernateConf(){
    Configuration config=new Configuration();
    //config.configure("hibernate.cfg.xml");
    config.configure();
    SessionFactory sf=config.buildSessionFactory();
    Session s=sf.openSession();
    Transaction tx=s.beginTransaction();
    SysUserGroup sysUserGroup=new SysUserGroup();
    sysUserGroup.getName();
    sysUserGroup.getPrincipal();
    sysUserGroup.getIncumbent();
    s.save(sysUserGroup);
    tx.commit();
    s.close();

}

}

这是我jdbc连接时的代码:
public class jdbc {
public static void main(String[] args) {
String url="jdbc:mysql://localhost:3306/cql";
String user="root";
String password="liangqichan";
String sql="Select * from sys_user_group";
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn=(Connection) DriverManager.getConnection(url, user, password);
st=(Statement) conn.createStatement();
rs=st.executeQuery(sql);

        while(rs.next()){
            System.out.println(rs.getString("id"));
            System.out.println(rs.getString("name"));
            System.out.println(rs.getString("remark"));
        }

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }


}

}

  • 写回答

7条回答 默认 最新

  • 星仔学习 2016-06-30 04:40
    关注

    还有就是你写的那个username明显错误,你写成uesrname了,改了以后应该就可以了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料