📄 webapp13.html
字号:
<a name="wp87709"> </a><p class="pBody">You can check that the table exists with the PointBase console tool as follows:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp87139"> </a><div class="pSmartList1"><li>In a terminal window, go to <code class="cCode"><</code><code class="cVariable">J2EE_HOME</code><code class="cCode">>/pointbase/tools/serveroption/</code>.</li></div><a name="wp87151"> </a><div class="pSmartList1"><li>Execute <code class="cCode">startconsole</code>.</li></div><a name="wp87153"> </a><div class="pSmartList1"><li>In the Connect to Database dialog </li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp113860"> </a><div class="pSmartList2"><li>Enter <code class="cCode">jdbc:pointbase:server://localhost/sun-appserv-samples</code> in the URL field<code class="cCode">.</code></li></div><a name="wp113866"> </a><div class="pSmartList2"><li>Enter <code class="cCode">PBPUBLIC</code> in the password field.</li></div></ol></div><a name="wp87734"> </a><div class="pSmartList1"><li>Click OK.</li></div><a name="wp87735"> </a><div class="pSmartList1"><li>Expand the SCHEMAS<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>PBPUBLIC<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>TABLES nodes. Notice that there is a table named BOOKS.</li></div><a name="wp87736"> </a><div class="pSmartList1"><li>To see the contents of the <code class="cCode">books</code> table:</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp87763"> </a><div class="pSmartList2"><li>In the Enter SQL commands text area, enter <code class="cCode">select * from books;</code>.</li></div><a name="wp87756"> </a><div class="pSmartList2"><li>Click the Execute button.</li></div></ol></div></ol></div><a name="wp215436"> </a><h3 class="pHeading2">Defining a Data Source in the J2EE Server</h3><a name="wp215437"> </a><p class="pBody">Data sources in the J2EE application server implement connection pooling. To define the Duke's Bookstore data source, you use the installed PointBase connection pool named PointBasePool.</p><a name="wp215438"> </a><p class="pBody">You create the data source with the J2EE 1.4 Application Server Admin Console, by following this procedure:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp215439"> </a><div class="pSmartList1"><li>Expand the JDBC node.</li></div><a name="wp215440"> </a><div class="pSmartList1"><li>Select the JDBC Resources node.</li></div><a name="wp215441"> </a><div class="pSmartList1"><li>Click the New... button.</li></div><a name="wp215442"> </a><div class="pSmartList1"><li>Type <code class="cCode">jdbc/BookDB</code> for the JNDI Name.</li></div><a name="wp215443"> </a><div class="pSmartList1"><li>Choose <code class="cCode">PointBasePool</code> for the Pool Name.</li></div><a name="wp215444"> </a><div class="pSmartList1"><li>Choose <code class="cCode">user</code> for the Resource Type.</li></div><a name="wp215445"> </a><div class="pSmartList1"><li>Select the Enabled checkbox.</li></div><a name="wp215446"> </a><div class="pSmartList1"><li>Click OK.</li></div></ol></div><a name="wp85541"> </a><h3 class="pHeading2">Configuring the Web Application to Reference a Resource</h3><a name="wp79319"> </a><p class="pBody">In order to access a database from a Web application, you must declare resource reference in the application's Web application deployment descriptor (see <a href="WebApp11.html#wp83876">Declaring References to Environment Entries, Resource Environment Entries, or Resources</a>). The resource reference declares a JNDI name, the type of the data resource, and the kind of authentication used when the resource is accessed. The JNDI name is used to create a data source object in the database helper class <code class="cCode">database.BookDB</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public BookDB () throws Exception { try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/BookDB"); con = ds.getConnection(); System.out.println("Created connection to database."); } catch (Exception ex) { System.out.println("Couldn't create connection." + ex.getMessage()); throw new Exception("Couldn't open connection to database: " +ex.getMessage()); } <a name="wp104229"> </a></pre></div><a name="wp104231"> </a><p class="pBody">To specify a resource reference to the bookstore data source with <code class="cCode">deploytool</code>:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp215523"> </a><div class="pSmartList1"><li>Select the WAR (created in chapters 11 through 15).</li></div><a name="wp215524"> </a><div class="pSmartList1"><li>Select the Resource Refs tab.</li></div><a name="wp215525"> </a><div class="pSmartList1"><li>Click Add.</li></div><a name="wp215526"> </a><div class="pSmartList1"><li>Type <code class="cCode">jdbc/BookDB</code> in the Coded Name field.</li></div><a name="wp215527"> </a><div class="pSmartList1"><li>Accept the default type <code class="cCode">javax.sql.DataSource</code>.</li></div><a name="wp215528"> </a><div class="pSmartList1"><li>Accept the default authorization <code class="cCode">Container</code>.</li></div><a name="wp215529"> </a><div class="pSmartList1"><li>Accept the default <code class="cCode">Sharable</code> selected.</li></div></ol></div><a name="wp77632"> </a><h3 class="pHeading2">Mapping the Web Application Resource Reference to a Data Source</h3><a name="wp215554"> </a><p class="pBody">The Web application resource reference and the data source defined in the J2EE 1.4 Application server both have JNDI names. You connect the resource reference name to the data source name with <code class="cCode">deploytool</code> as follows:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp215555"> </a><div class="pSmartList1"><li>Select the Resource Reference Name, <code class="cCode">jdbc/BookDB</code>, defined in the previous section.</li></div><a name="wp215556"> </a><div class="pSmartList1"><li>In the Sun-specific Settings frame, enter the name of the data source that you created in the application server, <code class="cCode">jdbc/BookDB</code>, in the JNDI Name field.</li></div></ol></div> </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="WebApp12.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="WebApp14.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 + -