EJB Access Database - EJB(Enterprise JavaBeans)

What is EJB Access Database?

In EJB 3.0, determination mechanism is used to access the database in which the container manages the database linked operations. Developers can access database using JDBC API call openly in EJB business methods.

To display database access in EJB, we need to perform the following responsibilities −

  • Step 1 − Create a table in the database.
  • Step 2 − Create a stateless EJB having business me.
  • Step 3 − Update stateless EJB. Add methods to add records and get records from database via entity manager.
  • Step 4 − A console based application client will access the stateless EJB to continue data in database.

Create Table

Create a table books in default database postures.

Create a Model Class

Create Stateless EJB

After construction the EJB section, we need a client to access the stateless bean, which we will be going to create in the next section.

Example Application

Let us construct a test EJB application to test EJB database access mechanism.

Step Description
1 Create a project with a nameEjbComponentunder a packagecom.Wisdomjobs.entityas explained in theEJB - Create Applicationchapter. You can also use the project created inEJB - Create Applicationchapter as such for this chapter to understand EJB data access concepts.
2 CreateBook.javaunder packagecom.Wisdomjobs.entityand modify it as shown below.
3 CreateLibraryPersistentBean.javaandLibraryPersistentBeanRemoteas explained in theEJB - Create Applicationchapter and modify them as shown below.
4 Clean and Build the application to make sure business logic is working as per the requirements.
5 Finally, deploy the application in the form of jar file on JBoss Application Server. JBoss Application server will get started automatically if it is not started yet.
6 Now create the EJB client, a console based application in the same way as explained in theEJB - Create Applicationchapter under topicCreate Client to access EJB. Modify it as shown below.

EJBComponent (EJB Module)




  • As soon as you deploy the EjbComponent project on JBOSS, notice the jobs log.
  • Jobs have repeatedly formed a JNDI entry for our session bean - LibraryPersistentBean/remote.
  • We will be using this lookup string to get remote business object of type −com.Wisdomjobs.stateless.LibraryPersistentBeanRemote

Jobs Application Server Log Output

EJBTester (EJB Client)


  • These properties are used to initialize the Initial Context object of java naming service.
  • Initial Context object will be used to lookup stateless session bean.


EJBTester performs the following tasks −

  • Load properties from jndi.properties and initialize the Initial Context object.
  • In testStatefulEjb () method, jade lookup is done with the name - "LibraryStatelessSessionBean/remote" to obtain the remote business object (tasteful EJB).
  • Then user is shown a library store User Interface and he/she is asked to enter a choice.
  • If the user enters 1, the system asks for book name and saves the book using stateless session bean add Book () method. Session Bean is persisting the book in database via Entity Manager Call.
  • If the user enters 2, the system retrieves books using stateless session bean get Books () method and exits.
  • Then another jade lookup is done with the name - "LibraryStatelessSessionBean/remote" to obtain the remote business object (tasteful EJB) again and listing of books is done.

Run Client to Access EJB

Locate EJBTester.java in development explorer. Right click on EJBTester class and decide on run file.

Confirm the following output in Net beans console.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

EJB(Enterprise JavaBeans) Topics