刚学hibernate出错.
是一个手动创建数据表的小程序.
[ShowDB.java]
package com.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 手动导出数据表
*
* @author puruidong
* @version 2013.8.30
*/
public class ShowDB {
public static void main(String[]args){
Configuration cfg = new Configuration().configure() ;//加载配置信息
SchemaExport export = new SchemaExport(cfg); //实例化SchemaExport.
System.out.println(export);
export.create(true,true);//到处数据表
}
}
package com.hibernate;
public class User {
private int num;
private String name;
private int age ;
private String address;
//省略set/get方法
}
<?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"><!-- dtd无法访问. -->
<hibernate-configuration>
<session-factory>
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库连接 -->
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/Test</property>
<!-- 数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 打印SQL语句 -->
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="com/hibernate/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
<?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">
<hibernate-mapping>
<class name="com.hibernate.User" table="Studentss" >
<id name="num" column="num" type="int">
<generator class="native"></generator>
</id>
<property name="name" type="string" length="20">
<column name="name"></column>
</property>
<property name="age" type="int">
<column name="age"></column>
</property>
<property name="address" type="string" length="200">
<column name="address"></column>
</property>
</class>
</hibernate-mapping>
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2096)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2008)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
at com.hibernate.ShowDB.main(ShowDB.java:17)
Caused by: org.dom4j.DocumentException: null Nested exception: null
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2088)
... 3 more
跟dtd不能访问有关系吗? hibernate
[解决办法]
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 改为
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
[解决办法]
找到hibernate-configuration-3.0.dtd源文件,去这个文件里查看一下开头怎么写的,直接复制过来就行,好像是在这个文件的注释部分里。。。