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

📄 ch03.html

📁 java2高级编程
💻 HTML
📖 第 1 页 / 共 4 页
字号:
    SearchHome shome=(SearchHome) ctx.lookup(&quot;search&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087727"></A>//Create remote interface for search Bean</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087728"></A>    Search search=shome.create();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087729"></A>//Call search method and pass the search string</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087730"></A>    Enumeration enum=(Enumeration) search.getMatchingItemsList(                                          searchString);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087731"></A>    addLine(&quot;&lt;TABLE BORDER=1 CELLPADDING=1 CELLSPACING=0&gt;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087732"></A>      &lt;TR&gt; &lt;TH&gt;Item&lt;TH&gt; &lt;TH&gt;Summary&lt;TH&gt;&lt;TH&gt;Current High bid&lt;TH&gt;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087733"></A>      &lt;TH&gt;Number of bids&lt;TH&gt;&lt;TH&gt;Closing Date&lt;TH&gt;&lt;TR&gt;&quot;, out);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087734"></A>//Iterate through search results</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087735"></A>    while ((enum != null) &amp;&amp; (enum.hasMoreElements())) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087736"></A>      while(enum.hasMoreElements(in)) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087737"></A>//Locate auction items</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087738"></A>        AuctionItem ai=ahome.findByPrimaryKey((AuctionItemPK)enum.                                               nextElement());</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087739"></A>      displayLineItem(ai, out);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087740"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087741"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087742"></A>    addLine(&quot;&lt;TABLE&gt;&quot;, out);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087743"></A>  } catch (Exception e) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087744"></A>    addLine(&quot;AuctionServlet Search Items error&quot;, out);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087745"></A>    System.out.println(&quot;AuctionServlet &lt;searchItems&gt;:&quot;+e);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087746"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087747"></A>  out.flush();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087748"></A>}</PRE><P CLASS="Body"><A NAME="pgfId-1087749"></A>Part 2: The <EM CLASS="CODE">searchItems</EM> method then uses the returned <EM CLASS="CODE">Enumeration</EM> list from Part 1 and <EM CLASS="CODE">AuctionItemBean</EM> (page 51) to retrieve each bean in turn by calling <EM CLASS="CODE">findByPrimaryKey</EM> on each primary key in the list. This is a container-managed search based on the <EM CLASS="CODE">finder</EM> mechanism described in Auction House Application (Chapter 2). </P><PRE CLASS="CODE"><A NAME="pgfId-1087750"></A>//Iterate through search resultswhile ((enum != null) &amp;&amp; enum.hasMoreElements())) {  while(enum.hasMoreElements(in)) {    //Locate auction items    AuctionItem ai=ahome.findByPrimaryKey((    AuctionItemPK)enum.nextElement());    displayLineItem(ai, out);  }}</PRE></DIV></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087752"></A><EM CLASS="B-code">SearchBean</EM><A NAME="80182"></A></H5><P CLASS="Body"><A NAME="pgfId-1087755"></A>The SearchBean class (page 54) defines a <A NAME="marker-1087753"></A>bean-managed search for the <A NAME="marker-1087754"></A>primary keys of auction items with summary fields that contain characters matching the search string. This bean establishes a database connection and provides the <EM CLASS="CODE">getMatchingItemsList</EM> and <EM CLASS="CODE">EJBCreate</EM> methods. A custom <EM CLASS="CODE">SearchBean</EM> is written because the default finder rules supplied by the EJB server do not allow a wild card SQL search string. This technique can also be used for more complex queries or queries that could be better optimized by the developer than those search queries generated by the EJB server.</P></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087756"></A>Database Connection</H5><P CLASS="Body"><A NAME="pgfId-1087759"></A><A NAME="marker-1087757"></A><A NAME="marker-1087758"></A>Because this bean manages its own database access and search, it has to establish its own database connection. It cannot rely on the container to do this. The database connection is established by instantiating a static <EM CLASS="CODE">Driver</EM><A NAME="marker-1087760"></A> class and providing the <EM CLASS="CODE">getConnection</EM><A NAME="marker-1087761"></A> method. The <EM CLASS="CODE">getConnection</EM> method queries the static <EM CLASS="CODE">DriverManager</EM> class for a registered database driver that matches the <A NAME="marker-1087762"></A>Uniform Resource Locator (URL). In this case, the URL is <EM CLASS="CODE">weblogic.jdbc.jts.Driver</EM>. </P><PRE CLASS="CODE"><A NAME="pgfId-1087763"></A>//Establish database connection  static {    new weblogic.jdbc.jts.Driver();  }  public Connection getConnection() throws SQLException {    return DriverManager.getConnection(&quot;jdbc:weblogic:jts:ejbPool&quot;);  }</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087764"></A>Get Matching Items List</H5><P CLASS="Body"><A NAME="pgfId-1087767"></A>The <EM CLASS="CODE">getMatchingItemsList</EM><A NAME="marker-1087765"></A> method looks up <EM CLASS="CODE">AuctionItemBean</EM><A NAME="marker-1087766"></A> and creates a <EM CLASS="CODE">PreparedStatement</EM><A NAME="marker-1087768"></A> object for querying the database for summary fields that contain the search string. Data is read from the database into a <EM CLASS="CODE">ResultSet</EM><A NAME="marker-1087769"></A>, stored in a <EM CLASS="CODE">Vector</EM>, and returned to <EM CLASS="CODE">AuctionServlet</EM>. </P><PRE CLASS="CODE"><A NAME="pgfId-1087770"></A>public Enumeration getMatchingItemsList(String searchString)                    throws RemoteException {  ResultSet rs = null;  PreparedStatement ps = null;  Vector v = new Vector();  Connection con = null;  try {//Get database connection    con=getConnection();</PRE><PRE CLASS="CODE-caption"><A NAME="pgfId-1087772"></A>//API Ref: <A NAME="21003"></A>PreparedStatement prepareStatement(String sql)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087775"></A>    <A NAME="marker-1087773"></A><A NAME="marker-1087774"></A>ps=con.prepareStatement(&quot;select id from auctionitems where&quot; +                             &quot;summary like ?&quot;);    ps.setString(1, &quot;%&quot;+searchString+&quot;%&quot;);//Execute database query    ps.executeQuery();//Get results set    rs = ps.getResultSet();//Get information from results set    AuctionItemPK pk;    while (rs.next()) {      pk = new AuctionItemPK();      pk.id = (int)rs.getInt(1);//Store retrieved data in vector      v.addElement(pk);    }    rs.close();    return v.elements();  } catch (Exception e) {    System.out.println(&quot;getMatchingItemsList: &quot;+e);    return null;   } finally {     try {     if (rs != null) {        rs.close();     }     if (ps != null) {        ps.close();     }     if (con != null) {        con.close();      }      } catch (Exception ignore) {}    }  }</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087776"></A>Create Method</H5><P CLASS="Body"><A NAME="pgfId-1087780"></A>The <EM CLASS="CODE">ejbCreate</EM><A NAME="marker-1087777"></A> <A NAME="marker-1087778"></A>method creates a <EM CLASS="CODE">javax.naming.InitialContext</EM><A NAME="marker-1087779"></A> object. This is a <A NAME="marker-1087781"></A>JNDI class that lets <EM CLASS="CODE">SearchBean</EM> access the database without relying on the container. </P><PRE CLASS="CODE"><A NAME="pgfId-1087782"></A>public void ejbCreate() throws CreateException,   RemoteException {  Properties p = new Properties();  p.put(Context.INITIAL_CONTEXT_FACTORY,                &quot;weblogic.jndi.TengahInitialContextFactory&quot;);  try {    ctx = new InitialContext(p);  } catch(Exception e) {    System.out.println(&quot;create exception: &quot;+e);  }}</PRE></DIV></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087784"></A><EM CLASS="B-code">SearchBean</EM><A NAME="65554"></A></H4><PRE CLASS="CODE"><A NAME="pgfId-1087787"></A><A NAME="marker-1087785"></A><A NAME="marker-1087786"></A>package search;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087788"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087789"></A>import java.rmi.RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087790"></A>import javax.ejb.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087791"></A>import java.util.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087792"></A>import java.text.NumberFormat;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087793"></A>import java.io.Serializable;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087794"></A>import javax.naming.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087795"></A>import auction.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087796"></A>import registration.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087797"></A>import java.sql.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087798"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087799"></A>public class SearchBean implements SessionBean {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087800"></A>    protected SessionContext sctx;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087801"></A>    Properties p = new Properties();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087802"></A>    Context ctx;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087803"></A>    static {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087804"></A>      new weblogic.jdbc.jts.Driver();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087805"></A>    }  </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087806"></A>  public Connection getConnection() throws SQLException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087807"></A>    return DriverManager.getConnection(&quot;jdbc:weblogic:jts:ejbPool&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087808"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087809"></A>  public Enumeration getMatchingItemsList(String searchString)                                        throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087810"></A>    //Search for matching item by comparing searchString to</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087811"></A>    //the database summary field and returning an enumerated</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087812"></A>    //list of the primary key auction id values.</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087813"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087814"></A>    ResultSet rs = null;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087815"></A>    PreparedStatement ps = null;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087816"></A>    Vector v = new Vector();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087817"></A>    Connection con = null;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087818"></A>    try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087819"></A>      AuctionItemHome home = (AuctionItemHome) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087820"></A>      ctx.lookup(&quot;auctionitems&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087821"></A>      con = getConnection();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087822"></A>      ps = con.prepareStatement(</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087823"></A>           &quot;select id from auctionitems where summary like ?&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087824"></A>      ps.setString(1, &quot;%&quot;+searchString+&quot;%&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087825"></A>      ps.executeQuery();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087826"></A>      rs = ps.getResultSet();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087827"></A>      AuctionItemPK pk;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087828"></A>      while(rs.next()) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087829"></A>        pk = new AuctionItemPK();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087830"></A>        pk.id = (int)rs.getInt(1);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087831"></A>        v.addElement(pk);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087832"></A>      }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087833"></A>      return v.elements();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087834"></A>    } catch (Exception e) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087835"></A>      System.out.println(&quot;getMatchingItemsList: &quot;+e);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087836"></A>      return null;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087837"></A>    } finally {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087838"></A>      try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087839"></A>        if(rs != null) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087840"></A>          rs.close();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087841"></A>        }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087842"></A>        if(ps != null) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087843"></A>          ps.close();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087844"></A>        }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087845"></A>        if(con != null) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087846"></A>          con.close();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087847"></A>        }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087848"></A>      } catch (Exception ignore) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087849"></A>          //An exception was thrown trying to </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087850"></A>          //close the used connections. The exception is ignored </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087851"></A>          //because there is no further need for the used connection.</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087852"></A>     }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087853"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087854"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087855"></A>  public void ejbCreate() throws CreateException, RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087856"></A>    Properties p = new Properties();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087857"></A>    p.put(Context.INITIAL_CONTEXT_FACTORY,</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087858"></A>                   &quot;weblogic.jndi.TengahInitialContextFactory&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087859"></A>    try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087860"></A>      ctx = new InitialContext(p);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087861"></A>    } catch(Exception e) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087862"></A>      System.out.println(&quot;create exception: &quot;+e);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087863"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087864"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087865"></A>  public void setSessionContext(SessionContext sctx) throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087866"></A>    this.sctx = sctx;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087867"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087868"></A>  public void unsetSessionContext() throws RemoteException {   </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087869"></A>    sctx = null;  </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087870"></A>  } </PRE><PRE CLASS="CODE-caption"><A NAME="pgfId-1087872"></A>//API Ref: <A NAME="72441"></A>void ejbRemove()</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087876"></A>  <A NAME="marker-1087873"></A><A NAME="javax.ejb.SessionBean class"></A><A NAME="SessionBean class"></A>public void ejbRemove() {}</PRE><PRE CLASS="CODE-caption"><A NAME="pgfId-1087878"></A>//API Ref: <A NAME="90036"></A>void ejbActivate()</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087880"></A>  <A NAME="marker-1087879"></A>public void ejbActivate() throws RemoteException { }</PRE><PRE CLASS="CODE-caption"><A NAME="pgfId-1087882"></A>//API Ref: <A NAME="13197"></A>void ejbPassivate()</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087884"></A>  <A NAME="marker-1087883"></A>public void ejbPassivate() throws RemoteException { }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087885"></A>}</PRE><P CLASS="Body"><A NAME="pgfId-1026120"></A>&nbsp;</P></DIV></BODY></HTML>

⌨️ 快捷键说明

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