import org.hibernate.*;
import org.hibernate.cfg.*;

public class Test
{
    public static void main(String[] args)
    {
        try
        {
            //通过Configuration获得一个SessionFactory对象
        	SessionFactory sf = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
            //打开一个Session
            Session session = sf.openSession();
            //开始一个事务
            Transaction tx = session.beginTransaction();
            //创建一个Student对象
            Student stu = new Student();
            //通过Student的setter方法改变它的属性
            //注意student_id不用我们设置
            stu.setStudent_name("zhangsan");
            stu.setStudent_age(18);
            //通过session的save()方法将Student对象保存到数据库中
            session.save(stu);
            //提交事务
            tx.commit();
            //关闭会话
            session.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }    
}
最近下载更多
venusit  LV8 2018年5月30日
hx4188  LV3 2018年4月26日
最代码官方  LV167 2017年8月29日
最近浏览更多
雷迪斯俺的乡亲们  LV11 3月23日
女王不该在山炮村养花  LV8 2023年4月16日
陨石承影  LV1 2022年10月21日
微信网友_6040315240812544  LV8 2022年10月20日
呵呵哈哈哈  LV10 2022年7月23日
酷呆欢  LV5 2021年11月24日
huangyangcheng  LV4 2021年1月22日
jimshao289015254  LV9 2020年6月23日
xu890123  LV6 2020年6月2日
123456nty  LV37 2020年5月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友