persontest.java

来自「这个是j2eejava web 编程精要十五讲的全部源码。对学习java web」· Java 代码 · 共 53 行

JAVA
53
字号
package hibernateTest;

import org.hibernate.*;
import org.hibernate.cfg.*; 
import java.io.File;

public class PersonTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Configuration cfg;
		SessionFactory sf;
		Session session;
		Transaction tx;
		
		Person person = new Person();
		person.setId(100);
		person.setName("Wu Jicheng");
		person.setSex("m");
		person.setAddress("18 Shuangqing Road, Beijing");
		
		try {
			
			File file = new File("D:\\eclipse3.2\\workspace\\Hibernate_Proj\\hibernate.cfg.xml");
			cfg = new Configuration().configure(file);
			
			sf = cfg.buildSessionFactory();
			
			session = sf.openSession();
			
			tx = session.beginTransaction();
			
			session.save(person);
			
			tx.commit();
			
			session.close();
			
			System.out.println("Data have been saved into database!");
			
			
		}catch(HibernateException e) {
			e.printStackTrace();
		}
		
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?