神笔马良2018 2016-06-29 20: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-29 20:40
    关注

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

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

报告相同问题?

悬赏问题

  • ¥15 pycharm倒入虚拟环境的时候,显示这个,但是我的虚拟环境已经创建了
  • ¥15 FPGA芯片60进制计数器
  • ¥15 前端js怎么实现word的.doc后缀文件在线预览
  • ¥20 macmin m 4连接iPad
  • ¥15 DBIF_REPO_SQL_ERROR
  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部