oding for a Client
  
 Client Codes can be JSP/ Servlet/ Swing/ Web Start/ XML/ Wireless / Palm anything ..........

The best thing about J2EE Architecture is Client Code more than EJB , what i liked about J2EE itself is once u write a busness logic layer it can be accessed by ur JSP web page , Java Swing Code on ur desktop, Web Start new funda from sun, when ur on mobile thru mobile phone, those touch screen gadgets , ATM machines where u can withdraw cash ........

WOW great.......

Write once used anywhere , some thing like a sl** used anywhere, anytime, by anyone and same fun and worth ur money ........

This chapter will focus on Accessing Database from JSP throught EJB

 
  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

  Modify Record : 1 html and 2 JSP Files Required
modcustomer.html save it in E:/Phone/html/ directory 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

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

  Delete Record : 1 html and 1 JSP Files Required
delcustomer.html save it in E:/Phone/html/ directory
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
  View Single Record
viewcustomer.html save it in E:/Phone/html/ directory

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
  View Multiple Records
viewrange.html save it in E:/Phone/html/ directory
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

  What Next => Available : NO
Chapter 7. Deploying EJBs and Web Components
 Reach me!
   My Web URL : http://www.oocities.org/james_smith73
   If you like this article and/or code mailme or Join our small Group of Java Programmers ,
Till we meet next time BYE     

  Java, J2EE, J2SE and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc.
in the United States and other countries.