JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven









Step By step Web-JPA program

  • Jpa is used standalone and server side both
  • In server side it can be used with Servlet Jsp and Ejb


  • In this tutorial we will make the program for Web Jpa.
  • For Web Jpa we need to make a conection oll on to the server.
  • In this case we are using the glassfish server.
  • Start the server in the eclipse.
  • Right click and select the View Admin Console.




Connection Pool Creation In the glassfish server
  • A  admin console will get opened in the browser.
  • Exapand the  Resources-->JDBC  node.
  • Now select the  Connection Pools node.
  • Click on the New Button to create the new Connection Pool



  • Type the name as  mysql1
  • Select the Resource Type  as javax.sql.ConnectionPoolDataSource
  • Select the Mysql from the database Vendor.



  • Click on the Next Button.
  • Add the following properties for the JDBC Resources.
  • Click on the Finish button.
  • It will create  the mysql1



JDBC Resource Creation

  • After connection  pool creation we will create Jdbc Resource
  • Expand the JDBC Resources node.
  • Click on the new Button.


  • Type the JNDI name as jndi1.
  • Select the  Pool name as mysql1(earlier created)



  • Click on the Finish Button.
  • It will create JDBC Resourceon the glassfish server
  • This  will be used in persistence.xml for datasource element

Verification of  the Connection Pool Created

  • Navigate to the Connection Pool node.
  • Click on the created  Connection mysql1.
  • Navigate to the General tab
  • Click on the Ping button.
  • If it shows the Ping Succeeded Message 
     it means it is properly created
  • Else it can show the error message





Dynamic Project

  • Create one dynamic project.
  • Type the name of the project as CustomerInfo.
  • Select the server as glassfish v3.




Adding JPA Facets
  • By default the dynamic project is not  JPA  properties.
  • So  user need to add the JPA  Facets to this project.
  • Right Click on the project.
  • Select the properties.
  • Here select the Project Facets.
  • Check the java Persistence.
  • Click on the Ok button.




Entities Creation

  • Using the jpa tool we can create the Entity file.
  • Right click on the project.
  • Select the JPA tools-->Generate Entities from tables.






Creating the Connection

  • Here we are using the  Mysql  connection
  • This connetion is previously created.
  • How to  create  a new connection step by step click the link  below


    How to  create  a new connection


    Creating the bean file
  • Select the schema test
  • Select the table as customer



  • Click on the Ok button.
  • It will create a new Entity(POJO) file.


import java.io.Serializable;
import javax.persistence.*;



@Entity
@Table(name="customer")
public class Customer implements Serializable {
  private static final long serialVersionUID = 1L;

  @Id
  private int cid;

  private String name;

    public Customer() {
    }

  public int getCid() {
    return this.cid;
  }

  public void setCid(int cid) {
    this.cid = cid;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

  • Open the persistence.xml.
  • Navigate to connection tab.
  • Type the JTA data source as jndi
  • This is the jdbc data source which we have created in the Admin console.




index.jsp
  • Now we will create a jsp file
  • In this put the code  for the client to execute.
  • In its action type cust_info


<html>
<body bgcolor=cyan>
<form action="cust_info">
<table>
  <tr>
    <td>Cid</td>
    <td><input type=text name="cid"></td>
  </tr>

  <tr>
    <td>Name</td>
    <td><input type=text name="name"></td>
  </tr>

  <tr>
    <td>select</td>
    <td>
    <select name="op1">
      <option value="add"/>add
      <option value="find"/>find
      <option value="update"/>update
      <option value="delete"/>delete
      <option value="findAll"/>findall
    </select>
    </td>
  </tr>
  <tr>
    <td><input type=submit value="ok"></td>
    <td><input type=reset value="clear"></td>
  </tr>
</table>
</form>
</body>
</html>



  • Now create one servlet cust_info.
  • put the code belowto add, show all, find, update and  remove the records.



import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class cust_info extends HttpServlet {

  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String op = request.getParameter("op1");

    Customer cst = null;

    try {
      EntityManagerFactory fac = Persistence.createEntityManagerFactory(
          "CustomerInfo", System.getProperties());
      EntityManager em = fac.createEntityManager();

      cst = new Customer();

      if (op.equals("add")) {
        int cid = Integer.parseInt(request.getParameter("cid"));
        String name = request.getParameter("name");

        cst.setCid(cid);
        cst.setName(name);
        
        em.getTransaction().begin();
        em.persist(cst);
        em.getTransaction().commit();
        out.print("record inserted");
      }

      else if (op.equals("delete")) {
        int cid = Integer.parseInt(request.getParameter("cid"));

        cst = em.find(Customer.class, cid);
        if (cst == null)
          out.print("id is wrong");
        else {
          em.getTransaction().begin();
          em.remove(cst);
          em.getTransaction().commit();
          out.print("record deleted");
        }
      else if (op.equals("find")) {
        int cid = Integer.parseInt(request.getParameter("cid"));

        em.getTransaction().begin();
        cst = em.find(Customer.class, cid);
        if (cst == null)
          out.print("id is wrong");
        else {
          out.print("<br><br>id is " + cst.getCid());
          out.print("<br><br>name  is " + cst.getName());

        }
      else if (op.equals("findAll")) {
        List<Customer> l = em.createQuery("select object(o) from 
            Customer as o"
)
            .getResultList();

        for (Customer cst1 : l) {
          out.print("<br><br>eid is " + cst1.getCid());
          out.print("<br>Name is " + cst1.getName());
        }

      else if (op.equals("update")) {
        int cid = Integer.parseInt(request.getParameter("cid"));
        String name = request.getParameter("name");

        cst = em.find(Customer.class, cid);
        if (cst == null)
          out.print("id is not present<br>cannot be updated");
        else {

          em.getTransaction().begin();
          cst.setCid(cid);
          cst.setName(name);

          em.merge(cst);
          em.getTransaction().commit();
          out.print(" record updated with eid " + cid);
        }
      }
      out.print("<center><br><br><br><br>
<input type=button value=back  onClick=javascript:history.back();>"
);
    catch (Exception e) {
      out.print(e);
    }

  }
}


Running the program.
  • Right Click on  the index.jsp.
  • Select the Run On server.
  • Select the glassfish server.













Download the code