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

📄 resources4.html

📁 j2eePDF格式的电子书
💻 HTML
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>Database Connections</title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="Resources3.html" />    <link rel="Next" href="Resources5.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="Resources3.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Resources5.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <blockquote><a name="wp80342"> </a><h2 class="pHeading1">Database Connections</h2><a name="wp80349"> </a><p class="pBody">The J2EE Application Server ships with a relational database product named PointBase. The following material shows how the <code class="cCode">SavingsAccountEJB</code> example of Chapter <a  href="BMP.html#wp79663">21</a> accesses a PointBase database. The <code class="cCode">SavingsAccountEJB</code> component is an entity bean with bean-managed persistence. </p><a name="wp80435"> </a><p class="pBody">Session beans and Web components will use the same approach as <code class="cCode">SavingsAccountEJB</code> to access a database. (Entity beans with container-managed persistence are different. See Chapter&nbsp;<a  href="CMP.html#wp79663">22</a>.)</p><a name="wp80350"> </a><h3 class="pHeading2">Coding a Database Connection</h3><a name="wp80351"> </a><p class="pBody">For the <code class="cCode">SavingsAccountEJB</code> example, the code that connects to the database is in the entity bean implementation class, <code class="cCode">SavingsAccountBean</code>. The source code for this class is in this directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/ejb/savingsaccount/src/</code><a name="wp80426"> </a></pre></div><a name="wp80424"> </a><p class="pBody">The bean connects to the database in three steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80352"> </a><div class="pSmartList1"><li>Specify the logical name of the database.</li></div><a name="wp80353"> </a><p class="pBodyRelative"><code class="cCode">&nbsp;&nbsp;private String dbName<br />&nbsp;&nbsp;&nbsp;&nbsp;= &quot;java:comp/env/jdbc/SavingsAccountDB&quot;;</code></p><a name="wp80354"> </a><p class="pBodyRelative">The<code class="cCode"> java:comp/env</code> portion of the logical name is the environment naming context of the component. The <code class="cCode">jdbc/SavingsAccountDB</code> string is the resource reference name (sometimes referred to as the coded name). In <code class="cCode">deploytool</code>, you specify the resource reference name and then map it to the JNDI name of the <code class="cCode">DataSource</code> object.</p><a name="wp80356"> </a><div class="pSmartList1"><li>Obtain the <code class="cCode">DataSource</code> object associated with the logical name.</li></div><a name="wp80357"> </a><p class="pBodyRelative"><code class="cCode">&nbsp;&nbsp;InitialContext ic = new InitialContext(); <br />&nbsp;&nbsp;DataSource ds = (DataSource) ic.lookup(dbName);</code></p><a name="wp80358"> </a><p class="pBodyRelative">Given the logical name for the resource, the <code class="cCode">lookup</code> method returns the <code class="cCode">DataSource</code> object that is bound to the JNDI name in the directory.</p><a name="wp80359"> </a><div class="pSmartList1"><li>Get the <code class="cCode">Connection</code> object from the <code class="cCode">DataSource</code> object.</li></div><a name="wp80360"> </a><p class="pBodyRelative"><code class="cCode">&nbsp;&nbsp;Connection con = ds.getConnection();</code></p></ol></div><a name="wp81695"> </a><h3 class="pHeading2">Defining a Data Source in the J2EE Application Server</h3><a name="wp81697"> </a><p class="pBody">In the previous section, you mapped the resource reference to the JNDI name of the data source. The <code class="cCode">deploytool</code> utility stores this mapping information in a deployment descriptor of <code class="cCode">SavingsAccountEJB</code>. In addition to setting the bean's deployment descriptor, you also need to define the data source in the J2EE Application Server. You can define a data source with the Admin Console.</p><a name="wp81754"> </a><p class="pBody">To create the data source with the Admin Console, follow this procedure:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81755"> </a><div class="pSmartList1"><li>Open the URL <code class="cCode">http://localhost:4848/asadmin</code> in a browser.</li></div><a name="wp81756"> </a><div class="pSmartList1"><li>Expand the JDBC node.</li></div><a name="wp81757"> </a><div class="pSmartList1"><li>Select the JDBC Resources node.</li></div><a name="wp81758"> </a><div class="pSmartList1"><li>Click New.</li></div><a name="wp81759"> </a><div class="pSmartList1"><li>Type <code class="cCode">jdbc/ejbTutorialDB</code> for the JNDI Name.</li></div><a name="wp81760"> </a><div class="pSmartList1"><li>Choose <code class="cCode">PointBasePool</code> for the Pool Name.</li></div><a name="wp81761"> </a><div class="pSmartList1"><li>Click OK.</li></div><a name="wp81762"> </a><div class="pSmartList1"><li>Note that <code class="cCode">jdbc/ejbTutorialDB</code> is listed under the JDBC Resources node.</li></div></ol></div><a name="wp79715"> </a><h3 class="pHeading2">Specifying a Resource Reference </h3><a name="wp80469"> </a><p class="pBody">The application for the <code class="cCode">SavingAccountEJB</code> example is in the <code class="cCode">SavingsAccountApp.ear</code> file, which is in this directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;<code class="cVariable">INSTALL&gt;</code><code class="cCode">/j2eetutorial14/examples/ejb/provided-ears/</code><a name="wp80476"> </a></pre></div><a name="wp80483"> </a><p class="pBody">For your convenience, the resource reference and JNDI names in <code class="cCode">SavingsAccountApp.ear</code> have already been configured in <code class="cCode">deploytool</code>. However, you may find it instructive to open <code class="cCode">SavingsAccountApp.ear</code> in <code class="cCode">deploytool</code> and follow these steps for specifying the resource reference.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79716"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, select <code class="cCode">SavingsAccountEJB</code> from the tree.</li></div><a name="wp79717"> </a><div class="pSmartList1"><li>Select the Resource Refs tab.</li></div><a name="wp79718"> </a><div class="pSmartList1"><li>Click Add.</li></div><a name="wp79719"> </a><div class="pSmartList1"><li>In the Coded Name field, enter <code class="cCode">jdbc/SavingsAccountDB</code>. </li></div><a name="wp79725"> </a><div class="pSmartList1"><li>In the Type combo box, select <code class="cCode">javax.sql.DataSource</code>.</li></div><a name="wp79727"> </a><div class="pSmartList1"><li>In the Authentication combo box, select Container.</li></div><a name="wp79728"> </a><div class="pSmartList1"><li>If you want other enterprise beans to share the connections acquired from the <code class="cCode">DataSource</code>, select the Sharable checkbox.</li></div><a name="wp81487"> </a><div class="pSmartList1"><li>To map the resource reference to the data source, enter <code class="cCode">jdbc/ejbTutorialDB </code>in the JNDI Name field.</li></div></ol></div><a name="wp79729"> </a><p class="pBody">If the preceding steps are followed, the Resource Refs tab will appear as shown in <a  href="Resources4.html#wp79738">Figure 26-1</a>. </p><a name="wp79736"> </a><p class="pBody"></p><div align="left"><img src="images/Fig55a.gif" height="439" width="410" alt="Resource Refs Tabbed Pane of SavingsAccountEJB" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="79738"> </a><strong><font >Figure 26-1    Resource Refs Tabbed Pane of <code class="cCode">SavingsAccountEJB</code></font></strong></p>    </blockquote>   <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="Resources3.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Resources5.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font>  </body></html>

⌨️ 快捷键说明

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