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

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 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下: