SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Creating BMP Entity Beans - Project

 Introduction

To solve the preceding problem, perform the following tasks:
  1. Configure a data source with the J2EE Application Server.
  2. Create the BMP entity bean home interface.
  3. Create the BMP entity bean remote interface.
  4. Create the BMP enity bean class.
  5. Create the Web Client.
  6. Package the BMP entity bean.
  7. Package the Web Client.
  8. Deploy the application.
  9. Test the application.

Types of Enterprise Beans are
  • Session Beans
    1. Stateless Session Beans
    2. Stateful Session Beans
  • Entity Beans
    1.  Bean Managed Persistent (BMP) Entity Beans
    2. Container Managed Persistent (CMP) Entity Beans
  • Message-Driven Bean
CLICK HERE to download this complete example (zip file)


 Create the BMP Entity Bean Home Interface (UserAccountHome.java)

import java.util.Collection;
import java.rmi.RemoteException;
import javax.ejb.*;
public interface UserAccountHome extends EJBHome
{
public UserAccount create(String id, String name, String address, String phoneNo) throws RemoteException, CreateException;
public UserAccount findByPrimaryKey(String id) throws FinderException, RemoteException;
public Collection findByName(String name) throws FinderException, RemoteException;
}
Download: UserAccountHome.java

 Create the BMP Entity Bean Remote Interface (UserAccount.java)

import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface UserAccount extends EJBObject
{
public String getName() throws RemoteException;
public String getAddress() throws RemoteException;
public String getPhoneNo() throws RemoteException;
}
Download: UserAccount.java

 Create the BMP Entity Bean Class (UserAccountBean.java)

import java.sql.*;
import javax.sql.*;
import java.util.*;
import java.math.*;
import javax.ejb.*;
import javax.naming.*;
public class UserAccountBean implements EntityBean
{
private String id;
private String name;
private String address;
private String phoneNo;
private EntityContext context;
private Connection con;
private final static String dbName = "java:comp/env/jdbc/UserAccountDB";
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public String getPhoneNo()
{
return phoneNo;
}
public String ejbCreate(String id, String name, String address, String phoneNo) throws CreateException
{
try
{
insertRow(id, name, address, phoneNo);
}catch (Exception ex)
{
throw new EJBException("ejbCreate: " + ex.getMessage());
}
this.id = id;
this.name = name;
this.address = address;
this.phoneNo = phoneNo;
return id;
}
public String ejbFindByPrimaryKey(String primaryKey) throws FinderException
{
boolean result;
try
{
result = selectByPrimaryKey(primaryKey);
}catch (Exception ex)
{
throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
}
if (result)
{
return primaryKey;
}
else
{
throw new ObjectNotFoundException ("Row for id " + primaryKey + " not found.");
}
}
public Collection ejbFindByName(String name) throws FinderException
{
Collection result;
try
{
result = selectByName(name);
}catch (Exception ex)
{
throw new EJBException("ejbFindByName " + ex.getMessage());
}
return result;
}
public void ejbRemove()
{
try
{
deleteRow(id);
}catch (Exception ex)
{
throw new EJBException("ejbRemove: " + ex.getMessage());
}
}
public void setEntityContext(EntityContext context)
{
this.context = context;
}
public void unsetEntityContext()
{}
public void ejbActivate()
{
id = (String) context.getPrimaryKey();
}
public void ejbPassivate()
{
id = null;
}
public void ejbLoad()
{
try
{
loadRow();
}catch (Exception ex)
{
throw new EJBException("ejbLoad: " + ex.getMessage());
}
}
public void ejbStore()
{
try
{
storeRow();
}catch (Exception ex)
{
throw new EJBException("ejbStore: " + ex.getMessage());
}
}
public void ejbPostCreate(String id, String name, String address, String phoneNo)
{}
private void makeConnection()
{
try
{
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
con = ds.getConnection();
}catch (Exception ex)
{
throw new EJBException("Unable to connect to database. " + ex.getMessage());
}
}
private void releaseConnection()
{
try
{
con.close();
}catch (SQLException ex)
{
throw new EJBException("releaseConnection: " + ex.getMessage());
}
}
private void insertRow(String id, String name, String address, String phoneNo) throws SQLException
{
makeConnection();
String insertStatement = "insert into useraccount values ( ? , ? , ? , ? )";
PreparedStatement prepStmt = con.prepareStatement(insertStatement);
prepStmt.setString(1, id);
prepStmt.setString(2, name);
prepStmt.setString(3, address);
prepStmt.setString(4, phoneNo);
prepStmt.executeUpdate();
prepStmt.close();
releaseConnection();
}
private void deleteRow(String id) throws SQLException
{
makeConnection();
String deleteStatement = "delete from savingsaccount where id = ? ";
PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
prepStmt.setString(1, id);
prepStmt.executeUpdate();
prepStmt.close();
releaseConnection();
}
private boolean selectByPrimaryKey(String primaryKey) throws SQLException
{
makeConnection();
String selectStatement = "select id " + "from useraccount where id = ? ";
PreparedStatement prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1, primaryKey);
ResultSet rs = prepStmt.executeQuery();
boolean result = rs.next();
prepStmt.close();
releaseConnection();
return result;
}
private Collection selectByName(String name) throws SQLException
{
makeConnection();
String selectStatement = "select id " + "from useraccount where name = ? ";
PreparedStatement prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1, name);
ResultSet rs = prepStmt.executeQuery();
ArrayList a = new ArrayList();
while (rs.next())
{
String id = rs.getString(1);
a.add(id);
}
prepStmt.close();
releaseConnection();
return a;
}
private void loadRow() throws SQLException
{
makeConnection();
String selectStatement = "select name, address, phoneno " + "from useraccount where id = ? ";
PreparedStatement prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1, id);
ResultSet rs = prepStmt.executeQuery();
if (rs.next())
{
this.name = rs.getString(1);
this.address = rs.getString(2);
this.phoneNo = rs.getString(3);
prepStmt.close();
}
else
{
prepStmt.close();
throw new NoSuchEntityException("Row for id " + id + " not found in database.");
}
releaseConnection();
}
private void storeRow() throws SQLException
{
makeConnection();
String updateStatement = "update useraccount set name = ? ," + "address = ?, phoneno = ?" + " where id = ?";
PreparedStatement prepStmt = con.prepareStatement(updateStatement);
prepStmt.setString(1, name);
prepStmt.setString(2, address);
prepStmt.setString(3, phoneNo);
prepStmt.setString(4, id);
int rowCount = prepStmt.executeUpdate();
prepStmt.close();
if (rowCount == 0)
{
throw new EJBException("Storing row for id " + id + " failed.");
}
releaseConnection();
}
}
Download: UserAccountBean.java

 Create the BMP Entity Bean to store the user information in the database (UserAccountServlet.java)

import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class UserAccountServlet extends HttpServlet
{
int id = 129;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String name = null;
String address = null;
String phoneNo = null;
name = request.getParameter("name");
address = request.getParameter("address");
phoneNo = request.getParameter("phoneNo");
PrintWriter out = response.getWriter();
response.setContentType("text/html");
try
{
Context initial = new InitialContext();
Object objref = initial.lookup("SimpleUserAccount");
UserAccountHome home = (UserAccountHome)PortableRemoteObject.narrow(objref, UserAccountHome.class);
id++;
UserAccount userAcc = home.create(id + "", name, address, phoneNo);
out.flush();
}catch (Exception ex)
{
out.println("Caught an exception." + "</BR>");
out.println(ex.toString());
}
}
}
Download: UserAccountServlet.java

 Create the Web Client (UserAccount.html)

<HTML>
<FORM ACTION = "http://localhost:8080/BMPAccountctx/servlet/UserAccountServlet">
<TABLE>
<TR>
<TD> Name </TD>
<TD> <INPUT TYPE = TEXT NAME= "name"> </TD>
</TR>
<TR>
<TD> Address </TD>
<TD> <INPUT TYPE = TEXT NAME= "address"> </TD>
</TR>
<TR>
<TD> Contact No </TD>
<TD> <INPUT TYPE = TEXT NAME= "phoneNo"> </TD>
</TR>
<TR>
<TD><INPUT TYPE = SUBMIT VALUE= "SUBMIT"> </TD>
</TR>
</TABLE>
</FORM>
</HTML>
Download: UserAccount.html


 Click Next To Continue ...
<- PREVIOUSNEXT ->