A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

test.java
public class Test {
        public static void main(String[] args) {
                Configuration cfg = new Configuration().configure();
                ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
                SessionFactory sf = cfg.buildSessionFactory(registry);
                Session session = sf.openSession();
                Transaction tx = session.beginTransaction();
                User user = new User();
                user.setName("张三");
                user.setPwd("1111");
                session.save(user);
                tx.commit();
                session.close();
        }
}

-------------------------------------------------------------------------------------------------------------------------------------------------
hibernate.cfg.xml
<!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>
        <!-- 配置数据库连接信息 -->
        <property name="connection.driver_class">
                com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
                jdbc:mysql://localhost:3306/hibernate4
        </property>
        <property name="connection.username">small_hong</property>
        <property name="connection.password">123456</property>
        <!-- 数据库方言 -->
        <property name="dialect">
                org.hibernate.dialect.MySQL5Dialect
        </property>
        <mapping resource="cn/siggy/pojo/User.hbm.xml" />
</session-factory>
</hibernate-configuration>

--------------------------------------------------------------------------------------
Uesr.java
public class User {
        private int id;
        private String name;
        private String pwd;
        public int getId() {
                return id;
        }
        public void setId(int id) {
                this.id = id;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public String getPwd() {
                return pwd;
        }
        public void setPwd(String pwd) {
                this.pwd = pwd;
        }
}

----------------------------------------------------------------------------------------------------
user.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
        <class name="cn.siggy.pojo.User" table="user">
                <id name="id">
                        <!-- 主键生成策略 -->
                        <generator class="native"></generator>
                </id>
                <!-- 实体类的属性 -->
                <property name="name"/>       
                <property name="pwd"/>       
        </class>
</hibernate-mapping>
----------------------------------------------------------------------------------------------------------------------

错误信息
四月 21, 2017 7:54:21 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
        at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:122)
        at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
        at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
        at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
        at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
        at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
        at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
        at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
        at cn.siggy.test.Test.main(Test.java:16)
Caused by: java.sql.SQLException: Access denied for user 'small_hong'@'localhost' (using password: YES)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:951)
        at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1717)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2395)
        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2428)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2213)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:797)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
        at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
        ... 13 more

---------------------------------------------------------------------------------------------------------------------------------------------------
用的是eclipse +Navicat+mysql
求大神搭救一下我  真的是奔溃了  真的。。。。。。。。

5 个回复

倒序浏览
弄了一天  还是不行  感觉脑子里面是一片浆糊!!!!!!!!!!
回复 使用道具 举报
你这代码写的繁琐呀,
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
你这个是想设置用户名和密码然后保存对对吧。先导包 然后创建表和实体类,然后orm元数据配置你都没错 核心配置也没错。测试 建议你用HibernateUtils工具类  获取session 开启事务 编写代码 保存 提交事务 关闭资源
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
胡坤鹏 发表于 2017-4-22 00:09
你这个是想设置用户名和密码然后保存对对吧。先导包 然后创建表和实体类,然后orm元数据配置你都没错 核心 ...

求指教,贴代码让我看一下可以么
回复 使用道具 举报
胡坤鹏 发表于 2017-4-22 00:05
你这代码写的繁琐呀,

有简单的?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马