首先,在src目录下,有一个文件,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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">FI_XIAO</property>
<property name="connection.url">
jdbc.url=jdbc:oracle:thin:@localhost:1521:FINANCE
</property>
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<property name="myeclipse.connection.profile">oracle</property>
<property name="connection.password">FI_XIAO</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="show_sql">true</property>
<mapping resource="com/system/SysUser.hbm.xml" />
</session-factory>
</hibernate-configuration>
其中,数据库名为:finance
2.
接下来,就是编写生成表的代码了,很简单,代码如下:
package utils;
import Java.io.File;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateSchemaExport {
static Session session;
?static Configuration config = null;
static Transaction tx = null;
?public static void main(String[] args) {
try {
config = new Configuration().configure(new File(
"src/hibernate.cfg.xml"));
System.out.println("Creating tables...");
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
System.out.println("Table created.");
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
}
}
}
在运行前,请先确定数据库服务已经开启,
运行结果如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Creating tables...
drop table if exists Message
drop table if exists ProjectInfo
drop table if exists about
drop table if exists gongshi
drop table if exists newslist
drop table if exists projectList
drop table if exists userlist
create table Message (id varchar(50) not null, msg text, adddate varchar(45) not null, ip varchar(45) not null, isflog varchar(2) not null, primary key (id))
create table ProjectInfo (id varchar(50) not null, names varchar(100) not null, info text not null, img text not null, primary key (id))
create table about (id varchar(50) not null, names varchar(45), tel varchar(45) not null, telcz varchar(45) not null, email varchar(100) not null, url varchar(200) not null, address text, primary key (id))
create table gongshi (id varchar(10) not null, info text not null, info2 text not null, primary key (id))
create table newslist (id varchar(50) not null, title varchar(100) not null, txt text not null, img text not null, adddate varchar(45) not null, downcount varchar(45) not null, ly varchar(45) not null, isflog varchar(45) not null, primary key (id))
create table projectList (id varchar(50) not null, img text not null, adddate varchar(45) not null, primary key (id))
create table userlist (id varchar(50) not null, names varchar(45) not null, pwd varchar(100) not null, regdate varchar(45) not null, flog varchar(2) not null, primary key (id))
Table created.
这样子,就完成了数据表的创建。