日期:2014-05-17  浏览次数:20685 次

刚学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);//到处数据表



}
}


[User.java]

package com.hibernate;

public class User {

private int num;
private String name;
private int age ;
private String address;
//省略set/get方法

}


[hibernate.cfg.xml]

<?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" />