JSTL SQL dateParam<sql:dateParam> Tag Example

The <sql:dateParam> tag is used as a nested action for <sql:query> and <sql:update> supply a date and time value for a value placeholder. If a null value is provided, the value is set to SQL NULL for the placeholder.

JSTL dateParam Tag provides the value of the date parameter. In SQL update statement, date paramter is sent using JSTL dateParam tag.

<sql:dateParam value="<string>" type="<string>"/>

JSTL Param Tag has following attribute.

1. value Attribute: Specifies the value of the date parameter.
2. type Attribute: Specifies the type of the parameter. It can be DATE , TIME or TIMESTAMP. TIMESTAMP is the default value.

JSTL SQL date param Tag Example:
To start with basic concept, let us create a simple table Students table in TEST database and create few records in that table as follows:

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="java.util.Date,java.text.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<title>JSTL sql:dataParam Tag</title>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     user="root"  password="pass123"/>

Date DoB = new Date("12-08-1987");
int empId = 1111;
<sql:update dataSource="${snapshot}" var="count">
   UPDATE Students SET dob = ? WHERE Id = ?
   <sql:dateParam value="<%=DoB%>" type="DATE" />
   <sql:param value="<%=studentId%>" />
<sql:query dataSource="${snapshot}" var="result">
   SELECT * from Students;
<table border="1" width="100%">
   <th>Emp ID</th>
   <th>First Name</th>
   <th>Last Name</th>
<c:forEach var="row" items="${result.rows}">
   <td><c:out value="${row.id}"/></td>
   <td><c:out value="${row.first}"/></td>
   <td><c:out value="${row.last}"/></td>
   <td><c:out value="${row.dob}"/></td>

Now try to access above JSP, which should display the following result:

Emp ID First Name Last Name DOB
1111 Dinesh Rajput 12-08-1987
2222 Sweety Rajput 20-11-1989

<<Previous <<   || Index ||   >>Next >>

No comments:

Post a Comment