日期:2014-05-20 浏览次数:20841 次
public class example { public void addUser(S s) throws HibernateException { //获得Hibernate session Session session = HibernateSessionFactory.getSession(); Transaction tx =session.beginTransaction(); //将s对象内容保存到数据库 session.save(s); session.flush(); tx.commit(); //关闭Hibernate session // HibernateSessionFactory.closeSession(); //session.close(); } public static void main(String[] msg) { example example = new example(); S s = new S(); s.setSno(57); s.setSname("1234"); try { example.addUser(s); } catch (HibernateException e) {} } }