神笔马良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条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多