|
Home
TOC Index |
|
SQL Tags
The JSTL SQL tags are designed for quick prototyping and simple applications. For production applications, database operations are normally encapsulated in JavaBeans components.
Table 16-7 SQL Tags Area
Function
Tags
TLD
Prefix
Database
setDataSource
/jstl-sql
sql
SQL
query
dateParam paramtransaction
update dateParam
param
The
setDataSourcetag is provided to allow you to set data source information for the database. You can provide a JNDI name orDriverManagerparameters to set the data source information. All the Duke's Bookstore pages that have more than one SQL tag use the following statement to set the data source:<sql:setDataSource dataSource="jdbc/BookDB" />The
querytag is used to perform an SQL query that returns a result set. For parameterized SQL queries, you use a nestedparamtag inside thequerytag.In
catalog.jsp, the value of theAddrequest parameter determines which book information should be retrieved from the database. This parameter is saved as the attribute namebidand passed to theparamtag. Notice that thequerytag obtains its data source from the context attributebookDSset in the context listener.<c:set var="bid" value="${param.Add}"/> <sql:query var="books" > select * from PUBLIC.books where id = ? <sql:param value="${bid}" /> </sql:query>The
updatetag is used to update a database row. Thetransactiontag is used to perform a series of SQL statements atomically.The JSP page
receipt.jsppage uses both tags to update the database inventory for each purchase. Since a shopping cart can contain more than one book, thetransactiontag is used to wrap multiple queries and updates. First the page establishes that there is sufficient inventory, then the updates are performed.<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" value="<%=(Integer)bookRow[7]%>" /> <c:if test="${item.quantity > inventory.quantity}"> <c:set var="sufficientInventory" value="false" /> <h3><font color="red" size="+2"> <fmt:message key="OrderError"/> There is insufficient inventory for <i><c:out value="${bookRow[3]}"/></i>.</font></h3> </c:if> </c:forEach> </c:forEach> <c:if test="${sufficientInventory == 'true'}" /> <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> <c:forEach var="bookRow" begin="0" items="${books.rows}"> <sql:update var="books" sql="update PUBLIC.books set inventory = inventory - ? where id = ?" > <sql:param value="${item.quantity}" /> <sql:param value="${bookId}" /> </sql:update> </c:forEach> </c:forEach> <h3><fmt:message key="ThankYou"/> <c:out value="${param.cardname}" />.</h3><br> </c:if> </sql:transaction>query Tag Result Interface
The
Resultinterface is used to retrieve information from objects returned from aquerytag.public interface Result public String[] getColumnNames(); public int getRowCount() public Map[] getRows(); public Object[][] getRowsByIndex(); public boolean isLimitedByMaxRows();For complete information about this interface, see the API documentation for the
javax.servlet.jsp.jstl.sqlpackage.The
varattribute set by aquerytag is of typeResult. ThegetRowsmethod returns an array of maps that can be supplied to theitemsattribute of aforEachtag. The JSTL expression language converts the syntax${result.rows}to a call toresult.getRows. The expression${books.rows}in the following example returns an array of maps.When you provide a array of maps to the
forEachtag, thevarattribute set by the tag is of typeMap. To retrieve information from a row, use theget("colname")method to get a column value. The JSTL expression language converts the syntax${map.colname}to a call tomap.get("colname"). For example, the expression${book.title}returns the value of the title entry of a book map.The Duke's Bookstore page
bookdetails.jspretrieves the column values from thebookmap as follows.<c:forEach var="book" begin="0" items="${books.rows}"> <h2><c:out value="${book.title}"/></h2> <fmt:message key="By"/> <em><c:out value="${book.firstname}"/> <c:out value="${book.surname}"/></em> (<c:out value="${book.year}"/>)<br> <br> <h4><fmt:message key="Critics"/></h4> <blockquote><c:out value="${book.description}"/> </blockquote> <h4><fmt:message key="ItemPrice"/>: <fmt:formatNumber value="${book.price}" type="currency"/> </h4> </c:forEach>The following excerpt from
catalog.jspuses theRowinterface to retrieve values from the columns of a book row using scripting language expressions. First the book row that matches a request parameter (bid) is retrieved from the database. Since thebidandbookRowobjects are later used by tags that use scripting language expressions to set attribute values and a scriptlet that adds a book to the shopping cart, both objects are declared as scripting variables using thejsp:useBeantag. The page creates a bean that describes the book and scripting language expressions are used to set the book properties from book row column values. Finally the book is added to the shopping cart.You might want to compare this version of
catalog.jspto the versions in JavaServer Pages Technology and Custom Tags in JSP Pages that use a book database JavaBeans component.<sql:query var="books" dataSource="${applicationScope.bookDS}"> select * from PUBLIC.books where id = ? <sql:param value="${bid}" /> </sql:query> <c:forEach var="bookRow" begin="0" items="${books.rowsByIndex}"> <jsp:useBean id="bid" type="java.lang.String" /> <jsp:useBean id="bookRow" type="java.lang.Object[]" /> <jsp:useBean id="addedBook" class="database.BookDetails" scope="page" /> <jsp:setProperty name="addedBook" property="bookId" value="<%=bookRow[0]%>" /> <jsp:setProperty name="addedBook" property="surname" value="<%=bookRow[1]%>" /> <jsp:setProperty name="addedBook" property="firstName" value="<%=bookRow[2]%>" /> <jsp:setProperty name="addedBook" property="title" value="<%=bookRow[3]%>" /> <jsp:setProperty name="addedBook" property="price" value="<%=((Double)bookRow[4]).floatValue()%>" /> <jsp:setProperty name="addedBook" property="year" value="<%=(Integer)bookRow[5]%>" /> <jsp:setProperty name="addedBook" property="description" value="<%=bookRow[6]%>" /> <jsp:setProperty name="addedBook" property="inventory" value="<%=(Integer)bookRow[7]%>" /> </jsp:useBean> <% cart.add(bid, addedBook); %> ... </c:forEach>
|
Home
TOC Index |
|
This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.
All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.