Home Jsp JSP SQL Tag

Related Tutorials


 
 

Share on Google+Share on Google+

JSP SQL Tag

Advertisement
JSTL?s database library supports database queries and transactions.

JSP SQL Tag

        

In this section you will learn how to use the jstl sql tag in jsp. 

JSTL?s database library supports database queries and transactions. JSP pages can import this library with the following directive:
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
You can see in the given example that in order to connect the jsp page to the database, we have used the jstl tag <sql:setDataSource> whose attributes are:
driver- Here you have to put the JDBC driver 
url- JDBC url for the database connection including database name
user-
the database username
password-
the database password

Understand with Example

The Tutorial illustrate an example from JSP SQL Tag. To understand the example we will show you to use jstl tag in jsp. The code consists of taglib directive that allow the jsp page to import  the library with the directive <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>. The example allow the jsp page to connect  to the database using <sql:setDataSource> specified with the given attribute. The attribute include name of the driver, url, user and password required to connect the database. The JSTL can read the data from the database using the tag <sql:query>. The code return the records from register table and print the corresponding values into your browser. 

c:out value="${}: The JSTL tag is used to print the fetched records from a table in the browser.

Here is the code of sql.jsp

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>
<sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://
   localhost:3306/register" user="root" password="root"/>
<html>
<body>
<sql:query var = "user" >
Select user_id,firstName,lastName,userName,email from registerTable
</sql:query>

<table border=1>
<c:forEach var="row" items="${user.rows}">
<tr>
<td><c:out value="${row.user_id}"/></td>
<td><c:out value="${row.firstName}"/></td>
<td><c:out value="${row.lastName}"/></td>
<td><c:out value="${row.userName}"/></td>
<td><c:out value="${row.email}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>

Output will be displayed as:

Download Source Code:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 3, 2009

Related Tutorials

Discuss: JSP SQL Tag   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Bhavana
March 26, 2011
Unable to read data.displays blank page

Hi, I tried to read data from oracle db and display it on the form using jstl <sql: querry>. I get a blank page, it doesnt even throw any error for me. Could you let me know where I am going wrong. It just doesnt go into forEach loop and data is not being displayed. Regards Bhavana
kalyani jagtap
October 8, 2012
hiii

best
DMCA.com