Java Reference
In-Depth Information
At this point we are ready to use the JSTL SQL library. Just like with any other tag
library, we need to add a taglib directive at the top of any JSP that will use the JSTL
SQL tags.
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
By convention, the prefix of sql is used for the JSTL SQL tag library, its uri
(Uniform Resource Identifier) is always http://java.sun.com/jsp/jstl/sql .
Retrieving Database Data with the
<sql:query> Tag
The first JSTL SQL tag that we will cover is the <sql:query> tag. This tag allows
us to execute an SQL SELECT statement, and store it in an object implementing the
javax.servlet.jsp.jstl.sql.Result interface. We can then iterate through this
object with the standard JSTL <c:forEach> tag.
The javax.servlet.jsp.jstl.sql.Result interface defines a number of methods
that we can call from a <c:forEach> tag in order to display database data on the
page. These methods are outlined in the following table.
Method Name
Description
getColumnNames()
Returns an array of String objects containing the
column names in the result set.
getRowCount()
Returns an int indicating the number of rows in the
result set.
getRows()
Returns an array of java.util.SortedMap
objects. Each element in the array represents a row in
the result set. Keys in each SortedMap are String
objects containing the column names; values are
objects representing the value for the column in the
current row.
getRowsByIndex()
Returns a bi-dimensional array of Objects
representing the rows and columns of the result set.
isLimitedByMaxRows()
Returns a boolean indicating if the maximum
number of rows in the result set was limited by the
maxRows attribute of the <sql:query> tag.
As we can see, all methods defined in the javax.servlet.jsp.jstl.sql.Result
interface are getter methods that conform to the JavaBean specification, therefore
these methods can be accessed as JavaBean properties from JSTL tags.
 
Search WWH ::




Custom Search