Monday, 3 February 2014

CRUD Application in Netbeans using EJB and JPA with MySQL



This tutorial demonstrates how to make an EJB application in Netbeans using JPA and the database as MYSQL.
This is an example of using CRUD (create, retrieve, update and delete) operations using JPA and MySQL database that follows the MVC architecture. It includes a login page which allows only registered users to login and a home page which has the CRUD features to add, edit, delete and search students from the database.

Before we start with the application we need the following:

Step 1: Create a database called 'mydatabase' using the MySQL workbench and create two tables called 'login' (which will store the usernames and passwords of users who can log in) and 'student' (which will store the details of students) and insert some data into the tables.

Login table


Student table



Step 2: Create a new Java Web Application project  and give any name such as 'MyEJBApplication' and select the server as 'Glassfish' (Mostly everything would be present by default)





Step 3: Create a JDBC Connection Pool (Right click on Project name -> New -> Other -> Glassfish(folder) -> JDBC Connection Pool) 


Step 4: Select MySQL from the dropdown box as shown below. ( Note the name of the connection pool) - in this case it is 'connectionPool')






















Step 5: Provide the host, port (within the URL parameter), username and password of your mysql database as shown below:




Step 6: Create a JDBC Resource (Right click on Project name -> New -> Other -> Glassfish(folder) -> JDBC Resource).




Step 7: Select the JDBC connection pool name you just in created above (step 4) from the drop down box. Provide a JNDI name as shown:



























Step 8: Create a Persistence Unit (Right click on Project name -> New -> Other -> Persistence -> Persistence Unit).



Step 9:  Provide a name to the Persistence Unit and select the data source name which you gave while creating the JDBC Resource above (step 7). (Note that the table generation strategy is set to 'None')



Step 10: Create 3 JSP Pages namely home.jsp, login.jsp and error.jsp and insert the following codes respectively

home.jsp



login.jsp



error.jsp




Step 11: Create 3 packages as follows:

Step 11.1: First package com.model and create 2 Entity beans namely 'Login' and 'Student' and insert the following codes respectively.

com.model.Login.java



com.model.Student.java



Step 11.2: Second package com.dao that contains the Session Beans (create by right click on project name -> New -> Session Bean) namely 'LoginDAO' and 'StudentDAO'. ( Both these session beans implement methods from a local interface. Select the 'Local Interface'option while creating the session beans. The LoginDAOLocal and StudentDAOLocal will be automatically created). Add the following respective codes to the session beans and their interfaces appropriately. 

com.dao.LoginDAO



com.dao.LoginDAOLocal



com.dao.StudentDAO



com.dao.StudentDAOLocal



Step 11.3: Third package com.controller that contains the servlets namely 'LoginServlet' and 'StudentServlet' and add the following codes respectively.

com.controller.LoginServlet

com.controller.StudentServlet


Step 12: Check your configuration files(web.xml, persistence.xml and glassfish-resources.xml). They should be as follows:

web.xml

persistence.xml

glassfish-resources.xml


Step 13: The final directory structure of the entire project should look as follows:


Step 14: Run the application by Right click on the project name -> Run

14.a: You should see login.jsp. Provide the username as 'admin' and password as 'admin' (as given in the login table in mydatabase) and click on Login.



14.b: You can now add, edit, delete or search for student records from the student table.





Notes:
  • The Java Persistence API (JPA) is one possible approach to ORM. JPA is a specification and several implementations are available. Popular implementations are Hibernate, EclipseLink and Apache OpenJPA. (You can check the implementation while creating the persistence.xml configuration file. In this case or by default in netbeans its EclipseLink).
  • The Entity beans represent the data in the database. Each instance of an entity bean corresponds to a row in the table. Session beans contain the actual business logic methods.