Saturday, June 7, 2014

Contoh Operasi CRUD ke Database MySQL Server menggunakan Java Persistance API (JPA)

  No comments
@PersistenceContext(unitName = "Persist1MVCPU")
 private EntityManager em;

@Resource
private javax.transaction.UserTransaction utx;
//Insert
public boolean addMhsToDatabase(TblMahasiswa mhs) {
        boolean res = false;
        try {
            utx.begin();
            em.persist(mhs);
            res = true;
            utx.commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e;
            throw new RuntimeException(e);
        }
        return res;
    }
//Load Data
public List getListMahasiswa(){
        return em.createNamedQuery("TblMahasiswa.findAll").getResultList();

}

No comments :