The <sql: dateParam> tag is used to add parameter to sql statement to the given java.util.Date value. This tag uses action of both <sql: query> and <sql: update> to provide data and time value for value placeholder.
Attributes of <sql: dateParam> Tag
- value: It is used to add value of the parameter for DATE or TIME in the data base table.
- type: It is used to specify type DATE,TIME or TIMESTAMP for the value attribute.
Example of <sql: dateParam> Tag
<%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/test" var="ds" user="root" password="" /> <% Date DoB = new Date ("1988/05/25"); int id = 200; %> <sql: update dataSource="${ds}" var="count"> UPDATE student SET dob=? WHERE id=? <sql: dateParam value="<%=DoB %>" type="DATE"></sql: dateParam> <sql: param value="<%=id %>"/> </sql:update> <sql:query dataSource="${ds}" var="result"> select * from student; </sql:query> <table border="1" width="300"> <tr> <th>ID</th> <th>NAME</th> <th>DOB</th> </tr> <c:forEach var="row" items="${result.rows}"> <tr> <td><c:out value="${row.id}" /></td> <td><c:out value="${row.name}" /></td> <td><c:out value="${row.dob}" /></td> </tr> </c:forEach> </table> </body> </html>
Details of the Code
- <sql: setDataSource var=”ds” driver=”com.mysql.jdbc.Driver” url=”jdbc: mysql: //localhost/test” user=”root” password=”” /> tag is used to connect to the data base and specify variable name for data source and driver name, url , user name and password of the database.
- >sql: update dataSource=”${ds}” var=”count”< tag is used to update database table student by using set and where condition.
- <sql: dateParam value=”” type=”DATE”/< tag is used to provide value of the date parameter using attributes such as value attribute which gives value of date parameter and type attribute which specifies type of parameter like DATE,TIME or TIMESTAMP.
- <sql: param value=””/> tag is used to provide value of the parameter using value attribute.
- <sql:query dataSource=”${ds}” var=”result” > select * from student; </sql:query> tag is used to fetch the data from the database table which takes attributes such as ,datasource attribute specifies the datasource, var attribute result of the SQL statement.
- <c:forEach var=”row” items=”${result.rows}”> tag is used to produce database table from SQL query and items attribute specifies collection of items to iterate in the loop.
Steps for Execution
- Save the file as DateParam.jsp in eclipse IDE.
- Now select the jsp file, right click on mouse and select Run as -> Run on Server.
Output
After successful execution of the program we will get the following result:
Previous Tutorial : JSTL SQL sql:param Tag :: Next Tutorial : JSTL SQL sql:transaction Tag