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

📄 webapp13.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<?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>Accessing Databases from Web Applications</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="WebApp12.html" />    <link rel="Next" href="WebApp14.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="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">    <blockquote><a name="wp69868"> </a><h2 class="pHeading1">Accessing Databases from Web Applications</h2><a name="wp83411"> </a><p class="pBody">Data that is shared between Web components and persistent between invocations of a Web application is usually maintained in a database. Web applications use the JDBC 2.0 API to access relational databases. For information on this API, see </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCode"><a  href="http://java.sun.com/docs/books/tutorial/jdbc" target="_blank">http://java.sun.com/docs/books/tutorial/jdbc</a></code><a name="wp83413"> </a></pre></div><a name="wp83407"> </a><p class="pBody">In the JDBC API, databases are accessed via <code class="cCode">DataSource</code> objects. A <code class="cCode">DataSource</code> has a set of properties that identify and describe the real world data source that it represents. These properties include information like the location of the database server, the name of the database, the network protocol to use to communicate with the server, and so on. </p><a name="wp83421"> </a><p class="pBody">Applications access a data source using a connection, and a <code class="cCode">DataSource</code> object can be thought of as a factory for connections to the particular data source that the <code class="cCode">DataSource</code> instance represents. In a basic <code class="cCode">DataSource</code> implementation, a call to the method <code class="cCode">DataSource.getConnection</code> returns a connection object that is a physical connection to the data source. </p><a name="wp148648"> </a><p class="pBody">If a <code class="cCode">DataSource</code> object is registered with a JNDI naming service, an application can use the JNDI API to access that <code class="cCode">DataSource</code> object, which can then be used to connect to the data source it represents. </p><a name="wp148665"> </a><p class="pBody">The Duke's Bookstore examples described in chapters 11 through 15 use the PointBase evaluation database included with the J2EE 1.4 Application Server to maintain the catalog of books. See the PointBase support site at </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCode"><a  href="http://www.pointbase.com/node.shtml?navHier=Support/Product+Docs&amp;CF=support/docs/overview.html" target="_blank">http://www.pointbase.com/node.shtml?navHier=Support/Product+Docs&amp;CF=support/docs/overview.html</a></code><a name="wp180941"> </a></pre></div><a name="wp180940"> </a><p class="pBody">for detailed information about the PointBase database.</p><a name="wp180935"> </a><p class="pBody">This section describes how to: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp83425"> </a><div class="pSmartList1"><li>Start the PointBase database server</li></div><a name="wp83426"> </a><div class="pSmartList1"><li>Populate the database</li></div><a name="wp83428"> </a><div class="pSmartList1"><li>Define a data source in the application server</li></div><a name="wp83429"> </a><div class="pSmartList1"><li>Configure a Web application to reference the data source with a JNDI name</li></div><a name="wp83430"> </a><div class="pSmartList1"><li>Map the JNDI name to the data source defined in the application server</li></div></ul></div><a name="wp83431"> </a><h3 class="pHeading2">Starting the PointBase Database Server</h3><a name="wp89032"> </a><p class="pBody">To start the PointBase database server:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp89033"> </a><div class="pSmartList1"><li>In a terminal window, go to<code class="cCode"> &lt;</code><code class="cVariable">J2EE_HOME</code><code class="cCode">&gt;/pointbase/tools/serveroption</code>.</li></div><a name="wp84827"> </a><div class="pSmartList1"><li>Execute the <code class="cCode">startserver</code> script.</li></div></ol></div><a name="wp215345"> </a><p class="pBody">On Windows, from the Start menu, choose</p><a name="wp215346"> </a><p class="pBody">      Programs<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Sun Microsystems<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>J2EE 1.4 SDK<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Start PointBase</p><a name="wp84828"> </a><h3 class="pHeading2">Populating the Example Database</h3><a name="wp79646"> </a><p class="pBody">To populate the database for the Duke's Bookstore examples:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp85491"> </a><div class="pSmartList1"><li>In a terminal window, go to <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/bookstore/</code>.</li></div><a name="wp85496"> </a><div class="pSmartList1"><li>Run <code class="cCode">asant</code> <code class="cCode">create-db_common</code>. This task runs a PointBase commander tool command to read the file <code class="cCode">books.sql </code>and execute the SQL commands contained in the file. The table named <code class="cCode">books</code> is created for the user <code class="cCode">pbpublic</code> in the <code class="cCode">sun-appserv-samples</code> PointBase database.</li></div><a name="wp79483"> </a><div class="pSmartList1"><li>At the end of the processing, you should see the following output:</li></div><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...[java] SQL&gt; INSERT INTO books VALUES(&#39;207&#39;, &#39;Thrilled&#39;, &#39;Ben&#39;,[java]  &#39;The Green Project: Programming for Consumer Devices&#39;,[java]  30.00, false, 1998, &#39;What a cool book&#39;, 20);[java] 1 row(s) affected[java] SQL&gt; INSERT INTO books VALUES(&#39;208&#39;, &#39;Tru&#39;, &#39;Itzal&#39;,[java]  &#39;Duke: A Biography of the Java Evangelist&#39;,[java]  45.00, true, 2001, &#39;What a cool book.&#39;, 20);[java] 1 row(s) affected<a name="wp87705"> </a></pre></div></ol></div>

⌨️ 快捷键说明

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