24. CRUD Operations Using JPA(Java Perssitance API)
Friday, February 4, 2011
In this Post we are going to learn the most powerful Database Framework called JPA.
Here I am going to demonstrate CRUD(Create, Retrieve, Update and Delete) operations.
For this create the fallowing directory structure in Netbeans and create the fallowing JSPs and Servlets, and also generate the Entity Class.
index.jsp
view.jsp
addDept.jsp
Now the code for ViewAllServlet.java is given below, and prior to that inject Persistence into the servlet.
Now, the code for DelDeptServlet.java is given below.
That's it folks !!!
I will get back to you with some more interesting topics !!
Here I am going to demonstrate CRUD(Create, Retrieve, Update and Delete) operations.
For this create the fallowing directory structure in Netbeans and create the fallowing JSPs and Servlets, and also generate the Entity Class.
index.jsp
view.jsp
addDept.jsp
Now the code for ViewAllServlet.java is given below, and prior to that inject Persistence into the servlet.
public class ViewAllServlet extends HttpServlet { @Resource private javax.transaction.UserTransaction utx; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { Context ctx = (Context) new InitialContext().lookup("java:comp/env"); utx.begin(); EntityManager em = (EntityManager) ctx.lookup("persistence/LogicalName"); //em.persist(object); Query q=em.createNamedQuery("Dept.findAll"); Listlist=q.getResultList(); utx.commit(); request.setAttribute("list", list); RequestDispatcher view=request.getRequestDispatcher("/view.jsp"); view.forward(request, response); } catch(Exception e) { out.print(e.toString()); } finally { out.close(); } }
Now, the code for DelDeptServlet.java is given below.
public class DelDeptServlet extends HttpServlet { @Resource private javax.transaction.UserTransaction utx; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String cmd=request.getParameter("cmd"); if(cmd.equals("Submit")) { Short deptno=new Short(request.getParameter("deptno")); String dname=request.getParameter("dname"); String loc=request.getParameter("loc"); Dept dept=new Dept(); dept.setDeptno(deptno); dept.setDname(dname); dept.setLoc(loc); Context ctx = (Context) new InitialContext().lookup("java:comp/env"); utx.begin(); EntityManager em = (EntityManager) ctx.lookup("persistence/LogicalName"); em.persist(dept); utx.commit(); RequestDispatcher view=request.getRequestDispatcher("/ViewAllServlet"); view.forward(request, response); } if(cmd.equals("Delete")) { String deptno=request.getParameter("deptno"); Context ctx = (Context) new InitialContext().lookup("java:comp/env"); utx.begin(); EntityManager em = (EntityManager) ctx.lookup("persistence/LogicalName"); if(deptno!=null) { Dept dept=em.find(Dept.class, new Short(deptno)); em.remove(dept); utx.commit(); RequestDispatcher view=request.getRequestDispatcher("/ViewAllServlet"); view.forward(request, response); } String str[] =request.getParameterValues("del"); if(str!=null) { for(int i=0;i<<str.length;i++) { Dept dept=em.find(Dept.class, new Short(str[i])); em.remove(dept); } utx.commit(); RequestDispatcher view=request.getRequestDispatcher("/ViewAllServlet"); view.forward(request, response); } else { out.print("You Must Select atleast one Record to Delete"); } } if(cmd.equals("Edit")) { Short deptno=new Short(request.getParameter("deptno")); Context ctx = (Context) new InitialContext().lookup("java:comp/env"); utx.begin(); EntityManager em = (EntityManager) ctx.lookup("persistence/LogicalName"); Dept dept=em.find(Dept.class, deptno); utx.commit(); request.setAttribute("dept", dept); RequestDispatcher view=request.getRequestDispatcher("/addDept.jsp"); view.forward(request, response); } if(cmd.equals("Update")) { Short deptno=new Short(request.getParameter("deptno")); String dname=request.getParameter("dname"); String loc=request.getParameter("loc"); Context ctx = (Context) new InitialContext().lookup("java:comp/env"); utx.begin(); EntityManager em = (EntityManager) ctx.lookup("persistence/LogicalName"); Dept dept=em.find(Dept.class, deptno); dept.setDname(dname); dept.setLoc(loc); em.merge(dept); utx.commit(); RequestDispatcher view=request.getRequestDispatcher("/ViewAllServlet"); view.forward(request, response); } } catch(Exception e) { out.print(e.toString()); } finally { out.close(); } }When we compile the above program we will get the fallowing output.
That's it folks !!!
I will get back to you with some more interesting topics !!
Nice tutorial, I learn a lot from this, but is it only used Entity classes ....how about using EJB 3 using facade methods....How can I use EJB and inject it using Remote or locale facade...
Thanks for the tutorial
hi sir
here we are updating(merge) the data using Query string
passing from jsp and get it in Edit servlet.
the string will appear in the url.
i think it is the security problem if we want to update student profile we allow to edit username, password like i think it will appear in the url
thank u sir