To perform a database query, you usually need to use a language called the SQL. The tag can use any valid SQL query to retrieve information from a database. SQL queries can appear either in the sql tag attribute or as the body of an tag.

When you use SQL, you'll find that it's common for a query to need a small bit of data filled in. For instance, in our case query is supplied by an id of a message board to display the messages for. Thus, sample below represent a page printing out the content of a given message flow:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>

<c:choose>

////////////// check for valid request:
	
	<c:when test="${empty param.messageBoard}">
		<font color="red">
			Error: viewMessages.jsp called incorrectly!
		</font>
	</c:when>

////////////// check whether database contains messages:

	<c:otherwise>
		<sql:query var="result">
			select * from messages
			where message_board = ?
			order by sent_date
			<sql:param value="${param.messageBoard}" />
		</sql:query>

		<c:choose>
			<c:when test="${result.rowCount == 0}">
				<p>
					Currently, there are no messages 
                                        in this message board.
					Be the first to post a message by 
                                        filling in the form below!
				</p>
			</c:when>
			<c:otherwise>
				<c:forEach items="${result.rows}" var="row">
				    <p>
                                      From: 
                                      <c:out value="${row.AUTHOR}" /> 
                                    </p>
				    <p>
                                      Date: 
                                      <c:out value="${row.SENT_DATE}" /> 
                                    </p>
				    <p>
                                      Subject: 
                                      <c:out value="${row.SUBJECT}" /> 
                                    </p>
					<blockquote>
					    <tt>
                                <c:out value="${row.BODY}" />
                                            </tt>
					</blockquote>
					<hr />
				</c:forEach>
			</c:otherwise>
		</c:choose>

////////////// let the user enter a message:

		<form method="post" action="postMessage.jsp">
			<p><b>New message</b> <br />
			Name: <input type="text" name="name" /> <br />
			Subject: <input type="text" name="subject" /> 
                        <br />
			<textarea cols="30" rows="5" name="body">
                        </textarea> 
                        <br />
			<input type="hidden" name="messageBoard"
			  value="<c:out value="${param.messageBoard}" />" 
                          />
			  <input type="submit" value="Post!" />
			</p>
		</form>
	</c:otherwise>
</c:choose>