Home Jsp JSP SQL Tag



JSP SQL Tag
Posted on: February 3, 2009 at 12:00 AM
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:

Related Tags for JSP SQL Tag:
cdatabaselibrarystldatatransactionsiojstlporttransactionactionsupporttabjsqueriesiebaseportselibactionslirarastrjesaseppssuatandaractrtrtssasacssristabndononsa


More Tutorials from this section

Ask Questions?    Discuss: JSP SQL Tag   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.