In this tutorial you will learn about the JSTL sql dateParam tag.
JSTL sql dateParam Tag
In this tutorial you will learn about the JSTL sql dateParam tag.
<sql:dateParam> tag is used to give the java.util.Date value as a parameter in SQL statements.
Attributes of dateParam :
- value : This is a required attribute to pass the value as a parameter for DATE, TIME, or TIMESTAMP column in database table.
- type : This is an optional attribute to specify the type date, time, timestamp for the value attribute.
Example :
An example is being given below will demonstrate you about the JSTL <sql:dateParam> tag. In this example at first I have created a table named student_info1 into which inserted some values in their respective fields. Then created a JSP page where used the <sql:setDataSource> tag to create data source in the next line used the <sql:query> tag to execute the query then designed a form where displayed the name and date_of_birth fields value from the table student_info1. Then takes the input fields of name and date_of_birth to modify the date_of_birth by name. Further checked for the null value of input text fields and parse the date and in the further steps used the <sql:update> tag to manipulated with the data used the update query to modify the date of date_of_birth field to give the date value I used the <sql:dateParam> tag. Then displayed the modified table.
Table student_info1
CREATE TABLE `student_info1` ( `roll_no` int(11) NOT NULL, `name` varchar(15) default NULL, `date_of_birth` date default NULL, PRIMARY KEY (`roll_no`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
JstlSqlDateParam.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.Date, java.text.Format, java.text.SimpleDateFormat" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>JSTL sql:setDateParam Example</title> </head> <body> <sql:setDataSource var="ds" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.10.13/data" user="root" password="root"/> <sql:query var="rs" dataSource="${ds}"> select name, date_of_birth from student_info1 </sql:query> <form> List of Student Name : <table border="1"> <c:forEach var="rowValue" items="${rs.rows}"> <tr> <td>${rowValue.name}</td> <td>${rowValue.date_of_birth}</td> </tr> </c:forEach> </table> <p>Enter name from the above list to change DoB : <input type="text" name="name"/></p> <p>Enter date of birth <input type="text" name="dt"/> <br>(yyyy-mm-dd)</p> <input type="submit" value="submit"/> </form> <% if(request.getParameter("name")!= null && request.getParameter("dt") != null) { String strDate = request.getParameter("dt"); SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date utilDate = formater.parse(strDate); java.sql.Date sqlDate = new Date(utilDate.getTime()); %> <sql:update dataSource="${ds}" var="upd"> update student_info1 set date_of_birth = ? where name = ? <sql:dateParam value="<%=sqlDate %>" type="DATE" /> <sql:param value="${param.name}" /> </sql:update> <sql:query var="rs" dataSource="${ds}"> select * from student_info1 </sql:query> <table border="1"> <tr> <td><b>roll_no</b></td> <td><b>name</b></td> <td><b>date_of_birth</b></td> </tr> <c:forEach var="rowValue" items="${rs.rows}"> <tr> <td>${rowValue.roll_no}</td> <td>${rowValue.name}</td> <td>${rowValue.date_of_birth}</td> </tr> </c:forEach> </table> <% } %> </body> </html>
How to run this example
Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :
- jstl.jar
- standard.jar
After adding of these jar files you may execute your program in the following ways :
- Select JstlSqlDateParam.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- On the Eclipse Editor go to your JstlSqlDateParam.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
- A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish
NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.
Output :
1. student_info1 table
2. When you will execute the above JSP page you will get the output on your eclipse browser as follows :
3. When you will input the value and when click on submit button you will get the output as follows :
[ 0 ] Comments