創(chuàng)建sessionBean 在昨天,我們完成了實(shí)體Bean,它實(shí)際上是映射到數(shù)據(jù)庫行并為行的每個(gè)列定義了getter和setter方法,今天我們來完成sessionBean。
BookControl.java的源代碼
package guestbook.maxuan.ejb.control;
import javax.ejb.*; import java.util.*; import java.rmi.*; import guestbook.maxuan.ejb.entity.value.*;
public interface BookControl extends javax.ejb.EJBObject { public void savely(BookValue value) throws RemoteException; public ArrayList getLyList() throws RemoteException; }
BookControlBean的源代碼
package guestbook.maxuan.ejb.control;
import javax.ejb.*; import java.util.*; import guestbook.maxuan.ejb.entity.*; import guestbook.maxuan.ejb.entity.value.*;
public class BookControlBean implements SessionBean { SessionContext sessionContext; public void ejbCreate() throws CreateException { /**@todo Complete this method*/ } public void ejbRemove() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } public void savely(BookValue value) { Book book=null; try { javax.naming.Context context = new javax.naming.InitialContext(); BookHome bookHome= (BookHome)context.lookup("Book"); if(value.getAction().equals("add")){ book=bookHome.create(value); }else{ try{ book=bookHome.findByPrimaryKey(value.getName()); book.setValue(value); }catch(FinderException fe){
} }
}catch (Exception ex) { throw new EJBException(ex); } } public ArrayList getLyList(){ ArrayList LyList=new ArrayList(); try { javax.naming.Context context = new javax.naming.InitialContext(); BookHome bookHome= (BookHome)context.lookup("Book"); Collection lyCntl = bookHome.findAll(); Iterator iterator = lyCntl.iterator(); while(iterator.hasNext()){ Book book = (Book) iterator.next(); LyList.add(book.getValue()); } }catch (Exception ex) { throw new EJBException(ex); } return LyList; } } BookControlHome的源代碼 package guestbook.maxuan.ejb.control;
import javax.ejb.*; import java.util.*; import java.rmi.*;
public interface BookControlHome extends javax.ejb.EJBHome { public BookControl create() throws CreateException, RemoteException; } 這樣我們就完成了sessionBean的編寫,它操作實(shí)體Bean賦于value的值, 這里比上次的教程多了寫入的操作!
出處:藍(lán)色理想
責(zé)任編輯:藍(lán)色
上一頁 [J2EE] 實(shí)戰(zhàn)留言本 [3] 下一頁 [J2EE] 實(shí)戰(zhàn)留言本 [5]
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|