⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02.html

📁 java2高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<PRE CLASS="CODE"><A NAME="pgfId-1087610"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087611"></A>import javax.ejb.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087612"></A>import java.rmi.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087613"></A>import java.util.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087614"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087615"></A>//Interface that is called by the client to create and return Beanspublic interface AuctionItemHome extends EJBHome {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087616"></A>  AuctionItem create(String theseller, String thedescription, int duration,                     double thestartprice, String thesummary)                      throws CreateException, RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087617"></A>  AuctionItem create(int id, String theseller, String thedescription,                      int duration, double thestartprice,                     String thesummary, double increment) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087618"></A>                     throws CreateException, RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087619"></A>  AuctionItem findByPrimaryKey(AuctionItemPK id) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087620"></A>                     throws FinderException, RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087621"></A>  public Enumeration findAllItems() throws FinderException, RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087622"></A>  public Enumeration findAllNewItems(java.sql.Date newtoday) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087623"></A>                     throws FinderException, RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087624"></A>  public Enumeration findAllClosedItems(java.sql.Date closedtoday) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087625"></A>                     throws FinderException, RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087626"></A>  public Enumeration findAllMatchingItems(String searchString) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087627"></A>                     throws FinderException, RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087628"></A>}</PRE><P CLASS="Body"><A NAME="pgfId-1087630"></A><EM CLASS="CODE">AuctionItemBean</EM><A NAME="marker-1087629"></A> (page 51) is the enterprise bean. It implements <EM CLASS="CODE">EntityBean</EM>, provides the business logic for the developer-defined methods, and implements <EM CLASS="CODE">EntityBean</EM> methods for creating the bean and setting the session context. This is a class that the bean developer needs to implement. Its field variables map to fields in the <EM CLASS="CODE">AUCTIONITEMS</EM> table shown just below. </P><P CLASS="Body"><A NAME="pgfId-1087633"></A><EM CLASS="CODE">AuctionItemPK</EM><A NAME="marker-1087631"></A> is the <A NAME="marker-1087632"></A>primary key class. The Enterprise JavaBeans server requires a container-managed entity bean to have a primary key class with a public primary key field (or fields, if using composite primary keys). The bean developer implements this class. The ID field is the primary key in the <EM CLASS="CODE">AUCTIONITEMS</EM> table shown just below, so the ID field is a public field in this class. The ID field is assigned a value when the primary key class is constructed. </P><PRE CLASS="CODE"><A NAME="pgfId-1087637"></A><A NAME="marker-1087634"></A><A NAME="marker-1087635"></A><A NAME="marker-1087636"></A>package auction;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087638"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087639"></A>//Class that is used to find auction items by their unique id</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087640"></A>public class AuctionItemPK implements java.io.Serializable {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087641"></A>  public int id;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087642"></A>}</PRE><P CLASS="Body"><A NAME="pgfId-1087643"></A>You can request the container manage database persistence for an enterprise bean or write the code to manage the persistence yourself. In this chapter, all beans (entity and session) are container-managed. With container-managed beans, all you do is specify which fields are container managed and let the Enterprise JavaBeans server do the rest. This is great for simple applications, but if you are coding something that is fairly complex, you might need more control. How to override the underlying Enterprise JavaBeans services to gain more control or provide similar services for non-Enterprise JavaBean applications is covered in Chapter 3, Data and Transaction Management (page 37). </P></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087644"></A>Auction Items Table</H5><P CLASS="Body"><A NAME="pgfId-1087646"></A>This is the schema for the <EM CLASS="CODE">AUCTIONITEMS</EM><A NAME="marker-1087645"></A> database table. Some application servers create the table for you, but others require you to create it yourself. The BEA Weblogic application server does not create the database table. </P><PRE CLASS="CODE"><A NAME="pgfId-1087647"></A>create table AUCTIONITEMS (SUMMARY VARCHAR(80) ,ID 							INT ,COUNTER							INT ,DESCRIPTION 							VARCHAR(1000) ,STARTDATE 							DATE ,ENDDATE 							DATE ,STARTPRICE 							DOUBLE PRECISION ,INCREMENT 							DOUBLE PRECISION ,SELLER 							VARCHAR(30) ,MAXBID 							DOUBLE PRECISION,BIDCOUNT 							INT,HIGHBIDDER 							VARCHAR(30) )</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087648"></A><EM CLASS="B-code">Registration</EM> Entity Bean</H5><P CLASS="Body"><A NAME="pgfId-1087651"></A><EM CLASS="CODE">RegistrationBean</EM><A NAME="marker-1087649"></A> consists of the same kinds of <A NAME="marker-1087650"></A>interfaces and classes and database table as the <EM CLASS="CODE">AuctionItem</EM> bean, but the actual business logic, database table fields, and primary key are different. Rather than describe the classes, you can browse them and refer back to the <EM CLASS="CODE">AuctionItem</EM> bean discussion if you have questions. </P><UL><LI CLASS="BL"><A NAME="pgfId-1087652"></A><EM CLASS="CODE">Registration</EM> </LI><LI CLASS="BL"><A NAME="pgfId-1087653"></A><EM CLASS="CODE">RegistrationHome</EM> </LI><LI CLASS="BL"><A NAME="pgfId-1087654"></A><EM CLASS="CODE">RegistrationBean</EM> </LI><LI CLASS="BL"><A NAME="pgfId-1087655"></A><EM CLASS="CODE">RegistrationPK</EM> </LI></UL></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087656"></A><EM CLASS="B-code">Registration</EM> Table</H5><P CLASS="Body"><A NAME="pgfId-1087658"></A>This is the schema for the <EM CLASS="CODE">REGISTRATION</EM><A NAME="marker-1087657"></A> database table. Some application servers create the table for you, but others require you to create it yourself. The BEA Weblogic application server does not create the database table.</P><PRE CLASS="CODE"><A NAME="pgfId-1087659"></A>create table REGISTRATION (THEUSER VARCHAR(40) ,PASSWORD 							VARCHAR(40) ,EMAILADDRESS 							VARCHAR(80) ,CREDITCARD 							VARCHAR(40) ,BALANCE 							DOUBLE PRECISION )</PRE></DIV></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087661"></A><A NAME="76838"></A>Session Bean Classes</H4><P CLASS="Body"><A NAME="pgfId-1087662"></A><EM CLASS="CODE">BidderBean</EM> and <EM CLASS="CODE">SellerBean</EM> are the session beans. <EM CLASS="CODE">BidderBean</EM> retrieves lists of auction items, searches for an item, checks the user ID and password when someone places a bid, and stores new bids in the database. <EM CLASS="CODE">SellerBean</EM> checks the user ID and password when someone posts an auction item, and adds new auction items to the database. </P><P CLASS="Body"><A NAME="pgfId-1087663"></A>Both session beans are initially deployed as stateless beans. A stateless bean does not keep a record of what the client did in a previous call; whereas, a stateful bean does. Stateful beans are very useful if the operation is more than a simple lookup and the client operation depends on something that happened in a previous call. </P><DIV><H5 CLASS="B"><A NAME="pgfId-1087664"></A><EM CLASS="B-code">Bidder</EM> Session Bean</H5><P CLASS="Body"><A NAME="pgfId-1087667"></A><A NAME="marker-1087665"></A>These are the <EM CLASS="CODE">BidderBean</EM><A NAME="marker-1087666"></A> interfaces and classes. There is no primary key class because these beans are transient and no database access is involved. To retrieve auction items from the database, <EM CLASS="CODE">BidderBean</EM> creates an instance of <EM CLASS="CODE">AuctionItemBean</EM>, and to process bids, it creates an instance of <EM CLASS="CODE">RegistrationBean</EM>. </P><UL><LI CLASS="BL"><A NAME="pgfId-1087668"></A><EM CLASS="CODE">Bidder</EM> </LI><LI CLASS="BL"><A NAME="pgfId-1087669"></A><EM CLASS="CODE">BidderHome</EM> </LI><LI CLASS="BL"><A NAME="pgfId-1087670"></A><EM CLASS="CODE">BidderBean</EM> </LI></UL><P CLASS="Body"><A NAME="pgfId-1087672"></A><EM CLASS="CODE">Bidder </EM><A NAME="marker-1087671"></A>is the remote interface. It describes what the bean does by declaring the developer-defined methods that provide the business logic for this bean. These methods are the ones that the client calls remotely. </P><PRE CLASS="CODE"><A NAME="pgfId-1087673"></A>package bidder;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087674"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087675"></A>import javax.ejb.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087676"></A>import java.rmi.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087677"></A>import java.util.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087678"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087679"></A>//Interface that contains the methods that can be called </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087680"></A>//on the bidder bean for placed auction bidspublic interface Bidder extends EJBObject {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087681"></A>  int placeBid(int item, String user, String password, double amount)               throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087682"></A>  Enumeration getItemList() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087683"></A>  Enumeration getNewItemList() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087684"></A>  Enumeration getClosedItemList() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087685"></A>   Enumeration getMatchingItemsList(String searchString) throws    RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087686"></A>}</PRE><P CLASS="Body"><A NAME="pgfId-1087688"></A><EM CLASS="CODE">BidderHome</EM><A NAME="marker-1087687"></A> is the home interface. It describes how the bean is created in, found in, and removed from its container. <EM CLASS="CODE">BidderBean</EM> (page 57) is the enterprise bean. It implements <EM CLASS="CODE">SessionBean</EM>, provides the business logic for the developer-defined methods, and implements <EM CLASS="CODE">SessionBean</EM> methods for creating the bean and setting the session context. </P><PRE CLASS="CODE"><A NAME="pgfId-1087689"></A>package bidder;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087690"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087691"></A>import javax.ejb.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087692"></A>import java.rmi.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087693"></A>import java.util.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087694"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087695"></A>//Interface that is called by the client to create Beanspublic interface BidderHome extends EJBHome {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087696"></A>  Bidder create() throws CreateException, RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087697"></A>}</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087698"></A><EM CLASS="B-code">Seller</EM> Session Bean</H5><P CLASS="Body"><A NAME="pgfId-1087701"></A><EM CLASS="CODE">SellerBean</EM><A NAME="marker-1087699"></A><A NAME="marker-1087700"></A> consists of the same kinds of interfaces and classes as <EM CLASS="CODE">BidderBean</EM>, except the business logic is different. Rather than describe the classes, you can browse them and refer back to the <EM CLASS="CODE">BidderBean</EM> discussion if you have questions. </P><UL><LI CLASS="BL"><A NAME="pgfId-1087702"></A> <EM CLASS="CODE">Seller</EM>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -