📄 jstl7.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>SQL Tags</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="JSTL6.html" /> <link rel="Next" href="JSTL8.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="JSTL6.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="JSTL8.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="wp63722"> </a><h2 class="pHeading1">SQL Tags</h2><a name="wp71159"> </a><p class="pBody">The JSTL SQL tags listed in <a href="JSTL7.html#wp84217">Table 14-8</a> are designed for quick prototyping and simple applications. For production applications, database operations are normally encapsulated in JavaBeans components.</p><div align="left"><table border="1" summary="SQL Tags" id="wp84217"> <caption><a name="wp84217"> </a><div class="pTableTitle">Table 14-8 SQL Tags </div></caption> <tr align="center"> <th><a name="wp84225"> </a><div class="pCellHeading">Area</div></th> <th><a name="wp84227"> </a><div class="pCellHeading">Function</div></th> <th><a name="wp84229"> </a><div class="pCellHeading">Tags</div></th> <th><a name="wp84231"> </a><div class="pCellHeading">Prefix</div></th></tr> <tr align="left"> <td colspan="1" rowspan="2"><a name="wp84233"> </a><div class="pCellBody">Database</div></td> <td><a name="wp84235"> </a><div class="pCellBody"> </div></td> <td><a name="wp84237"> </a><div class="pCellBody"><code class="cCode">setDataSource</code></div></td> <td colspan="1" rowspan="2"><a name="wp84239"> </a><div class="pCellBody"><code class="cCode">sql</code></div></td></tr> <tr align="left"> <td><a name="wp84243"> </a><div class="pCellBody">SQL</div></td> <td><a name="wp84245"> </a><div class="pCellBody"><code class="cCode">query</code></div><a name="wp84246"> </a><div class="pCellBody"><code class="cCode"> dateParam<br /> param<br />transaction</code></div><a name="wp84247"> </a><div class="pCellBody"><code class="cCode">update<br /> dateParam</code></div><a name="wp84248"> </a><div class="pCellBody"><code class="cCode"> param</code></div></td></tr></table></div><p class="pBody"></p><a name="wp67498"> </a><p class="pBody">The <code class="cCode">setDataSource</code> tag is provided to allow you to set data source information for the database. You can provide a JNDI name or <code class="cCode">DriverManager</code> parameters to set the data source information. All of the Duke's Bookstore pages that have more than one SQL tag use the following statement to set the data source:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><sql:setDataSource dataSource="jdbc/BookDB" /><a name="wp82764"> </a></pre></div><a name="wp82761"> </a><p class="pBody">The <code class="cCode">query</code> tag is used to perform an SQL query that returns a result set. For parameterized SQL queries, you use a nested <code class="cCode">param</code> tag inside the <code class="cCode">query</code> tag. </p><a name="wp67907"> </a><p class="pBody">In <code class="cCode"><a href="../examples/web/bookstore4/web/bookcatalog.txt" target="_blank">bookcatalog.jsp</a></code>, the value of the <code class="cCode">Add</code> request parameter determines which book information should be retrieved from the database. This parameter is saved as the attribute name <code class="cCode">bid</code> and passed to the <code class="cCode">param</code> tag. Notice that the <code class="cCode">query</code> tag obtains its data source from the context attribute <code class="cCode">bookDS</code> set in the context listener. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:set var="bid" value="${param.Add}"/><sql:query var="books" > select * from PUBLIC.books where id = ? <sql:param value="${bid}" /></sql:query><a name="wp76167"> </a></pre></div><a name="wp76187"> </a><p class="pBody">The <code class="cCode">update</code> tag is used to update a database row. The <code class="cCode">transaction</code> tag is used to perform a series of SQL statements atomically. </p><a name="wp77080"> </a><p class="pBody">The JSP page book<code class="cCode">receipt.jsp</code> page uses both tags to update the database inventory for each purchase. Since a shopping cart can contain more than one book, the <code class="cCode">transaction</code> tag is used to wrap multiple queries and updates. First the page establishes that there is sufficient inventory, then the updates are performed.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:set var="sufficientInventory" value="true" /><sql:transaction> <c:forEach var="item" items="${sessionScope.cart.items}"> <c:set var="book" value="${item.item}" /> <c:set var="bookId" value="${book.bookId}" /> <sql:query var="books" sql="select * from PUBLIC.books where id = ?" > <sql:param value="${bookId}" /> </sql:query> <jsp:useBean id="inventory" class="database.BookInventory" /> <c:forEach var="bookRow" begin="0" items="${books.rowsByIndex}"> <jsp:useBean id="bookRow" type="java.lang.Object[]" /> <jsp:setProperty name="inventory" property="quantity"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -