//这是测试的代码
package hibernatetest; import hibernate_1.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; public class HibernateDemo { @Test public void testAdd(){ //第一步加载核心配置文件 Configuration cfg=new Configuration();//创建 cfg对象 cfg.configure();//用对象加载配置文件 // 第二步 创建SessionFactory //读取核心配置文件内容,过程中,根据映射关系,在配置数据库里创建表 SessionFactory sf=cfg.buildSessionFactory(); // 第三步 使用SessionFactory创建session对象 //类似于数据库链接 Session se=sf.openSession(); // 第四部 开启事务 Transaction tx=se.beginTransaction(); // 第五步 写具体逻辑crud操作 User user=new User(); user.setName("林青霞"); user.setAddress("祥云镇"); user.setMale("女"); se.save(user); // 第六步 提交事务 tx.commit(); // 第七步 关闭资源 se.close(); sf.close(); } }
下面是实体类代码
package hibernate_1; public class User { private int uid; private String name; private String address; private String male; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getMale() { return male; } public void setMale(String male) { this.male = male; } }
下面是映射文件代码
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 映射配置文件对位置和名称无要求,但是建议:位置在实体类的包下;名字为 类名.hbm.xml --> <hibernate-mapping> <!-- name属性的值为实体类的全路径,即:包.类 --> <class name="hibernate_1.User" table="t_user"> <!-- id和property的column属性可以不用配置,默认会与实体类的属性名称相同 --> <id name="uid" column="uid"> <!-- 设置主键自增长 --> <generator class="native"></generator> </id> <!-- property里有Type属性,表示生成表的字段类型,默认与实体类的类型对应 --> <property name="name" column="name"></property> <property name="address" column="address"></property> <property name="male" column="male"></property> </class> </hibernate-mapping>
下面是jebernate.cfg.xml的代码
<?xml version="1.0" encoding="UTF-8"?> <!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///family</property> <property name="hibernate.connertion.username">root</property> <property name="hibernate.connertion.password">123</property> <!-- 第二部分:配置hibernate信息,可选的 --> <!--输出底层sql语句 --> <property name="hibernate.show_sql">true</property> <!--输出底层sql语句格式 --> <property name="hibernste.format_sql">true</property> <!--hibernate帮创建表,需要在配置之后 update:如果有表,更新;如果没表,创建 --> <property name="hibernate.hbm2ddl.auto">update</property> <!--配置数据库方言 让hibernate识别不同数据库特有的方言--> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!--第三部分:把映射文件放到核心配置文件中 --> <mapping resource="hibernate_1/User.hbm.xml"/> </session-factory> </hibernate-configuration>
下面是项目截图:
以下是错误信息:
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 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at hibernatetest.HibernateDemo.testAdd(HibernateDemo.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:911)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3953)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
... 37 more
at hibernatetest.HibernateDemo.testAdd(HibernateDemo.java:18)
这个类18行是什么?
SessionFactory sf=cfg.buildSessionFactory();
@南孤雁: Caused by: java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
感觉是hibernate配置文件有问题,看看数据库密码对不
@揽星: 检查好几遍了,这些都没问题,帐号密码在cmd都能访问的。网上查的同类问题说是hibernate的版本问题。引用了4的版本和5的版本都报错,但是报出来的错误不一样,蒙圈了啊
@南孤雁: 把你项目给我发来,我试试看!
@揽星: 你邮箱多少啊 博客园里面没有找到上传文件的地方
@南孤雁: 17600348207@163.com
东西发过去了,抱歉刚才才看见邮件,不好意思了
<property name="hibernate.connertion.username">root</property> <property name="hibernate.connertion.password">123</property>
connertion
这个单词写错了,应该是connection的
以后还是把做成功一次的保存起来,用的时候复制配置文件就好了,纯手打容易出错
@揽星: 我去 太感谢你了 我记得我是复制的就没有检查那里 应该是复制的时候不小心碰到了。。。很感谢
感觉这里不正常