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

📄 bmp5.html

📁 jdbc书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
and
<A HREF="./Code/auction/AuctionItemBean.java">AuctionItemBean</A> 
to retrieve each Bean in turn by calling
<CODE>findByPrimaryKey</CODE> on each primary
key in the list. This is a container-managed 
search based on the <CODE>finder</CODE> mechanism described in Chapter 2. 

</FONT>

<PRE>
//Iterate through search results
while ((enum != null) &amp;&amp; 
    enum.hasMoreElements())) {
 while(enum.hasMoreElements(in)) {

//Locate auction items
 AuctionItem ai=ahome.findByPrimaryKey((
    AuctionItemPK)enum.nextElement());
 displayLineItem(ai, out);
 }
}
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<A NAME="bean"></A>
<H3>SearchBean</H3>

The <A HREF="./Code/search/SearchBean.java">SearchBean.java</A>
class defines a Bean-managed search for the primary keys
of auction items with <CODE>summary</CODE> fields that
contain characters matching the search string. This Bean  
establishes a database connection, and 
provides the <CODE>getMatchingItemsList</CODE> and 
<CODE>EJBCreate</CODE> methods. 

<H4>Database Connection</H4>

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.

<P>
The database connection is established by instantiating a static
<CODE>Driver</CODE> class and providing the 
<CODE>getConnection</CODE> method.
The <CODE>getConnection</CODE> method queries the static
<CODE>DriverManager</CODE> class for a registered database driver
that matches the Uniform Resource Locator (URL) . In this case, the URL is
<CODE>weblogic.jdbc.jts.Driver</CODE>.

</FONT>

<PRE>
//Establish database connection
  static {
    new weblogic.jdbc.jts.Driver();
  }

  public Connection getConnection()
        throws SQLException {
    return DriverManager.getConnection(
        "jdbc:weblogic:jts:ejbPool");
  }
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<H4>Get Matching Items List</H4>

The <CODE>getMatchingItemsList</CODE> method
looks up <CODE>AuctionItemsBean</CODE>
and creates a <CODE>PreparedStatement</CODE> object for querying
the database for <CODE>summary</CODE> fields that contain 
the search string. Data is read from the database into a
<CODE>ResultSet</CODE>, stored in a <CODE>Vector</CODE>, and
returned to <CODE>AuctionServlet</CODE>.

</FONT>

<PRE>
  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();
//Create a prepared statement for database query
      ps=con.prepareStatement("select id from 
	auctionitems where summary like ?");
      ps.setString(1, "%"+searchString+"%");
//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("getMatchingItemsList: 
			    "+e);
	return null;
    }finally {
      try {
	 if(rs != null) {
           rs.close();
         }
	 if(ps != null) {
           ps.close();
         }
	 if(con != null) {
           con.close();
         }
      } catch (Exception ignore) {}
    }
  }
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<H4>Create Method</H4>

The <CODE>ejbCreate</CODE> method creates an
<CODE>javax.naming.InitialContext</CODE> object. This is a
Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Naming and Directory (JNDI) class that lets
<CODE>SearchBean</CODE> access the database without
relying on the container.

</FONT>

<PRE>
  public void ejbCreate() throws CreateException, 
    RemoteException {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, 
	"weblogic.jndi.TengahInitialContextFactory");
    try{
	ctx = new InitialContext(p);
    }catch(Exception e) {
	System.out.println("create exception: "+e);
    }
  }
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P ALIGN="RIGHT">
<FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT>

</FONT>
</TD>
</TR>
</TABLE>




<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->

</TD>
</TR>
</TABLE>

<!-- Copyright Insert -->

<BR CLEAR="ALL">

<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">   
  <TR>
    <TD VALIGN="TOP">
	
    <P ALIGN=CENTER>
    <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">
    [ This page was updated: <!-- new date --> 13-Oct-99 ]</font></P>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>
  
  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/products/">Products &amp; APIs</A> | 
    <A HREF="/developer/index.html">Developer Connection</A> | 
    <A HREF="/developer/infodocs/index.shtml">Docs &amp; Training</A> | 
    <A HREF="/developer/support/index.html">Online Support</A><BR>
    <A HREF="/developer/community/index.html">Community Discussion</A> |
    <A HREF="http://java.sun.com/industry/">Industry News</A> | 
    <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | 
    <A HREF="http://java.sun.com/casestudies">Case Studies</A>
    </FONT>
    </CENTER>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>

  <TR>
    <TD ALIGN="CENTER">
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - 
    <A HREF="http://java.sun.com/applets/">Applets</A> - 
    <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - 
    <A HREF="http://java.sun.com/jobs/">Employment</A> - 
    <A HREF="http://java.sun.com/nav/business/">Business &amp; Licensing</A> - 
    <A HREF="http://java.sun.com/javastore/">Java Store</A> -
    <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>
    </FONT>
    </TD>
  </TR>

  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <a href="/siteinfo/faq.html">FAQ</a> |
    <a href="/feedback/index.html">Feedback</a> | 
    <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | 
    <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
    </FONT>
    </CENTER>

    </TD>
  </TR>
  
  <TR>
    <TD>

    <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
      <TR>
        <TD WIDTH="50%">
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        For more information on Java technology<BR>
        and other software from Sun Microsystems, call:<BR>
        </FONT>
        <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">
        (800) 786-7638<BR></FONT>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Outside the U.S. and Canada, dial your country's 
        <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&amp;T&nbsp;Direct&nbsp;Access&nbsp;Number</A> first.<BR>
        </FONT>
        </TD>

        <TD ALIGN="RIGHT" WIDTH="50%">
        <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Copyright &copy; 1995-99
        <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
        All Rights Reserved. 
        <a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>. 
        <A HREF="http://www.sun.com/privacy/">Privacy&nbsp;Policy</A>.
        </FONT>
        </TD>
      </TR>
    </TABLE>
	
    </TD>
  </TR> 
</TABLE>
</FORM>

<!-- End Copyright Insert -->


</BODY>
</HTML>

⌨️ 快捷键说明

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