Add Record : 1 html and 1 JSP File Required
addcustomer.html save it in E:/Phone/html/ directory
addcustresp.jsp save it in E:/Phone/jsp/ directory
1. Connect to Home interface using JNDI Lookup
2. home.create() pass the variable values for the Record to be inserted
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
// Call Create Method --------------------------------------------------------------------
MR = home.create(CustId, CustName, CustPh, CustPlan);
Complete Code Below
James Smith's Java : james_smith73@yahoo.com
<%@ page import="javax.naming.*" %>
<%@ page import="javax.rmi.PortableRemoteObject" %>
<%@ page import="EJB.ManageCust.*" %>
<%
String CustId = request.getParameter("CustId");
String CustName=request.getParameter("CustName");
String CustPh=request.getParameter("CustPh");
String CustPlan=request.getParameter("CustPlan");
boolean success;
try
{
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
System.out.println("home is : "+home);
ManageCustRemote MR = null;
// Call Create Method --------------------------------------------------------------------
MR = home.create(CustId, CustName, CustPh, CustPlan);
// Verify Details Below : --------------------------------------------------------------------------
MR = home.findByPrimaryKey(CustId);
CustName = MR.getCustName();
CustPh = MR.getCustPh();
CustPlan = MR.getCustPlan();
success = true;
}
catch (Exception ex)
{
success = false;
System.out.println("Customer ID Already Exists or Blank Data Entry , so Could not be Created ");
System.err.println("Caught an exception.");
ex.printStackTrace();
}
%>
<%
if (success)
{
%>
Customer Id
<%=CustId%>
Customer Name
<%=CustName%>
Customer Phone
<%=CustPh%>
Customer Plan
<%
int iCustPlan = Integer.parseInt(CustPlan);
if (iCustPlan == 1)
{
%>
General
Super
Economy
Special
<%
}
else if(iCustPlan == 2)
{
%>
General
Super
Economy
Special
<%
}
else if(iCustPlan == 3)
{
%>
General
Super
Economy
Special
<%
}
else
{
%>
General
Super
Economy
Special
<%
}
%>
Message Box !
Customer Created Successfully
<%
}
else
{
%>
Message Box !
Customer ID Already Exists / Invalid Customer ID Entry null or not number
<%
}
%>
Modify Record : 1 html and 2 JSP Files Required
modcustomer.html save it in E:/Phone/html/ directory
James Smith's Java : james_smith73@yahoo.com
modcust.jsp save it in E:/Phone/jsp/ directory
Before modification its important to identify the existing record and display the current values
i mean select before u run update query
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
// Verify Details Below : --------------------------------------------------------------------------
MR = home.findByPrimaryKey(CustId);
CustId = MR.getCustId();
CustName = MR.getCustName();
CustPh = MR.getCustPh();
CustPlan = MR.getCustPlan();
Complete code below
James Smith's Java : james_smith73@yahoo.com
<%@ page import="javax.naming.*" %>
<%@ page import="javax.rmi.PortableRemoteObject" %>
<%@ page import="EJB.ManageCust.*" %>
<%
String CustId = request.getParameter("CustId");
String CustName = null;
String CustPh = null;
String CustPlan = null;
int BillId = 0;
int NoOfCalls = 0;
double BillAmt = 0;
boolean success = false;
try
{
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
// Verify Details Below : --------------------------------------------------------------------------
MR = home.findByPrimaryKey(CustId);
CustId = MR.getCustId();
CustName = MR.getCustName();
CustPh = MR.getCustPh();
CustPlan = MR.getCustPlan();
success = true;
}
catch (Exception ex)
{
ex.printStackTrace();
success = false;
}
%>
<%
if (success)
{
%>
Message Box !
Customer Found Successfully
<%
}
else
{
%>
Message Box !
Customer ID Does Not Exists or Blank Data Entry , so No Data Retrieved
<%
}
%>
modcustresp.jsp save it in E:/Phone/jsp/ directory
Update the database record for the corresponding value of CutomerID [Primary Key]
and retrieve the new values from database after updation process
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
MR = home.findByPrimaryKey(nCustId);
// Update values in database
MR.setCustId(nCustId);
MR.setCustName(nCustName);
MR.setCustPh(nCustPh);
MR.setCustPlan(nCustPlan);
// Retrive the Values back after update
MR = home.findByPrimaryKey(nCustId);
nCustId = MR.getCustId();
nCustName = MR.getCustName();
nCustPh = MR.getCustPh();
nCustPlan = MR.getCustPlan();
Complete code below
James Smith's Java : james_smith73@yahoo.com
<%@ page import="javax.naming.*" %>
<%@ page import="javax.rmi.PortableRemoteObject" %>
<%@ page import="EJB.ManageCust.*" %>
<%
String nCustId = request.getParameter("CustId");
String nCustName=request.getParameter("CustName");
String nCustPh=request.getParameter("CustPh");
String nCustPlan=request.getParameter("CustPlan");
boolean success = false;
try
{
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
MR = home.findByPrimaryKey(nCustId);
// Update values in database
MR.setCustId(nCustId);
MR.setCustName(nCustName);
MR.setCustPh(nCustPh);
MR.setCustPlan(nCustPlan);
// Retrive the Values back after update
MR = home.findByPrimaryKey(nCustId);
nCustId = MR.getCustId();
nCustName = MR.getCustName();
nCustPh = MR.getCustPh();
nCustPlan = MR.getCustPlan();
success = true;
}
catch (Exception ex)
{
System.out.println("Customer ID Already Exists or Blank Data Entry , so Could not be Created ");
System.err.println("Caught an exception.");
ex.printStackTrace();
success = false;
}
%>
<%
if (success)
{
%>
Message Box !
Modified Successfully
<%
}
else
{
%>
Message Box !
Modification Failed
<%
}
%>
Delete Record : 1 html and 1 JSP Files Required
delcustomer.html save it in E:/Phone/html/ directory
James Smith's Java : james_smith73@yahoo.com
Connect to JNDI lookup
use home.remove() passing the Primary key , so that record can be deleted from the database
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
// Verify Details Below : --------------------------------------------------------------------------
MR = home.findByPrimaryKey(CustId);
CustName = MR.getCustName();
CustPh = MR.getCustPh();
CustPlan = MR.getCustPlan();
home.remove(CustId);
delcustresp.jsp save it in E:/Phone/jsp/ directory
James Smith's Java : james_smith73@yahoo.com
<%@ page import="javax.naming.*" %>
<%@ page import="javax.rmi.PortableRemoteObject" %>
<%@ page import="EJB.ManageCust.*" %>
<%
String CustId = request.getParameter("CustId");
String CustName = null;
String CustPh = null;
String CustPlan = null;
boolean success = false;
try
{
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
// Verify Details Below : --------------------------------------------------------------------------
MR = home.findByPrimaryKey(CustId);
CustName = MR.getCustName();
CustPh = MR.getCustPh();
CustPlan = MR.getCustPlan();
home.remove(CustId);
success = true;
}
catch (Exception ex)
{
System.out.println("Customer ID Already Exists or Blank Data Entry , so Could not be Created ");
System.err.println("Caught an exception.");
ex.printStackTrace();
}
if (success)
{
%>
Message Box !
Customer Deleted Successfully
values found before Customer Deletion were
CustId : <%=CustId%> CustName : <%=CustName%> CustPlan : <%=CustPlan%>
<%
}
else
{
%>
Message Box !
Could not Delete Customer / Customer Id not found to be deleted
<%
}
%>
View Single Record
viewcustomer.html save it in E:/Phone/html/ directory
James Smith's Java : james_smith73@yahoo.com
A single record is found by using its primary key , the below code will run a select query to identify the record related to it
in the database
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
// Verify Details Below : --------------------------------------------------------------------------
MR = home.findByPrimaryKey (CustId);
CustId = MR.getCustId();
CustName = MR.getCustName();
CustPh = MR.getCustPh();
CustPlan = MR.getCustPlan();
viewcust.jsp be stored in the E:/Phone/jsp/ directory
James Smith's Java : james_smith73@yahoo.com
<%@ page import="javax.naming.*" %>
<%@ page import="javax.rmi.PortableRemoteObject" %>
<%@ page import="EJB.ManageCust.*" %>
<%
String CustId = request.getParameter("CustId");
String CustName = null;
String CustPh = null;
String CustPlan = null;
boolean success = false;
try
{
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
// Verify Details Below : --------------------------------------------------------------------------
MR = home.findByPrimaryKey(CustId);
CustId = MR.getCustId();
CustName = MR.getCustName();
CustPh = MR.getCustPh();
CustPlan = MR.getCustPlan();
success = true;
}
catch (Exception ex)
{
ex.printStackTrace();
success = false;
}
%>
<%
if (success)
{
%>
Message Box !
Customer Found Successfully
<%
}
else
{
%>
Message Box !
Customer ID Does Not Exists or Blank Data Entry , so No Data Retrieved
<%
}
%>
View Multiple Records
viewrange.html save it in E:/Phone/html/ directory
James Smith's Java : james_smith73@yahoo.com
since we are seaching a range of recods we need to give upper and lower limit of customer Id value
we get records back from EJB as a Collection or Array , we need to decode them into single records
Save viewrangeresp.jsp in E:/Phone/jsp directory
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
// Call Create Method --------------------------------------------------------------------
int ilowerlim = Integer.parseInt(lowerlim);
int iupperlim = Integer.parseInt(upperlim);
Collection CRArray = home.findInRange(ilowerlim,iupperlim);
Iterator it = CRArray.iterator();
...............
while (it.hasNext())
{
Object objRef2 = it.next();
MR = (ManageCustRemote)PortableRemoteObject.narrow(objRef2, ManageCustRemote.class);
CustId = MR.getCustId();
CustName = MR.getCustName();
CustPh = MR.getCustPh();
CustPlan = MR.getCustPlan();
cntr++;
// Display Records in a table here --------
}
..............
Complete Code Below
James Smith's Java : james_smith73@yahoo.com
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.rmi.PortableRemoteObject" %>
<%@ page import="EJB.ManageCust.*" %>
<%
String lowerlim=request.getParameter("lowerlim");
String upperlim = request.getParameter("upperlim");
String CustId = null;
String CustName = null;
String CustPh = null;
String CustPlan = null;
int NoOfCalls = 0;
boolean success = false;
int cntr = 0;
try
{
// Connect to JNDI lookup --------------------------------------------------------------------
Context initial = new InitialContext();
Object objRef = initial.lookup("ManageCustBeanJNDI");
ManageCustHome home = (ManageCustHome) PortableRemoteObject.narrow(objRef, ManageCustHome.class);
ManageCustRemote MR = null;
// Call Create Method --------------------------------------------------------------------
int ilowerlim = Integer.parseInt(lowerlim);
int iupperlim = Integer.parseInt(upperlim);
Collection CRArray = home.findInRange(ilowerlim,iupperlim);
Iterator it = CRArray.iterator();
%>
Customer ID
Customer Name
Phone
Plan
<%
while (it.hasNext())
{
Object objRef2 = it.next();
MR = (ManageCustRemote)PortableRemoteObject.narrow(objRef2, ManageCustRemote.class);
CustId = MR.getCustId();
CustName = MR.getCustName();
CustPh = MR.getCustPh();
CustPlan = MR.getCustPlan();
cntr++;
%>
<%=CustId%>
<%=CustName%>
<%=CustPh%>
<%=CustPlan%>
<%
}
%>
<%
success = true;
}
catch (Exception ex)
{
System.out.println("Invalid Range , so no Records to View ");
System.err.println("Caught an exception.");
ex.printStackTrace();
}
%>
<%
if (success && cntr > 0)
{
%>
Message Box !
<%=cntr%> Customer(s) Found Successfully
<%
}
else
{
%>
Message Box !
Could not find Customers please check to and from range to be a valid number
From number must be less than To number of Customer ID
<%
}
%>
What Next => Available : NO
Chapter 7. Deploying EJBs and Web Components
Reach me!
Java, J2EE, J2SE and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc.
in the United States and other countries.