java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips arrow JavaServer Pages arrow JSTL - Accessing database source
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4100
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
JSTL - Accessing database source E-mail
User Rating: / 11
PoorBest 

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>


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.