Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.javaweb.service; import com.javaweb.hibernate.util.HibernateUtil; import com.javaweb.model.Nguoidung; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author DuongNguyen */ public class NguoidungService { //Cp nht ng?i dng public boolean InsertNguoidung(Nguoidung nd) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = null; try { tx = session.getTransaction(); tx.begin(); session.saveOrUpdate(nd); tx.commit(); return true; } catch (Exception e) { if (tx != null) { tx.rollback(); } System.out.println(e.toString()); } finally { session.close(); } return false; } //Ly thng tin ng?i dng theo IDTaikhoan public Nguoidung GetUserByIdNguoidung(int idNguoidung) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = null; try { tx = session.getTransaction(); tx.begin(); String strQuery = "from Nguoidung where idnguoidung = " + idNguoidung; Query query = session.createQuery(strQuery); Nguoidung nd = (Nguoidung) query.uniqueResult(); tx.commit(); return nd; } catch (Exception e) { if (tx != null) { tx.rollback(); } System.out.println(e.toString()); } finally { session.close(); } return null; } //Xa Ng?i dng public boolean DeleteNguoidung(int idND) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = null; try { tx = session.getTransaction(); tx.begin(); Nguoidung nd = new Nguoidung(); nd.setIdnguoidung(idND); session.delete(nd); tx.commit(); return true; } catch (Exception e) { if (tx != null) { tx.rollback(); } System.out.println(e.toString()); } finally { session.close(); } return false; } }