JSTL SQL sql:dateParam Tag

JSTL sql:param Tag for beginners and professionals with examples on JSTL core tags, function tags, formatting tags, SQL tags, and miscellaneous tags.

JSTL SQL sql:dateParam Tag

JSTL SQL sql:dateParam Tag

The <sql:dateParam> is used to set the specified date for SQL query parameter.

It is used as nested tag for <sql:update> and <sql:query> to provide the date and time value for SQL query parameter. If null value is provided, the value is set at SQL NULL.

Example:

  1. <%  
  2. Date DoB = new Date("2000/10/16");  
  3. int studentId = 151;  
  4. %>  
  5. <sql:update dataSource="${db}" var="count">  
  6.    UPDATE Student SET dob = ? WHERE Id = ?  
  7.    <sql:dateParam value="<%=DoB%>" type="DATE" />  
  8.    <sql:param value="<%=studentId%>" />  
  9. </sql:update>  

JSTL SQL <sql:dateParam> Complete Example

Consider the below information about your MySQL database setup:

  • We are using the JDBC MySQL driver
  • We are using the test database on local machine
  • We are using the "root" as username and "1234" as password to access the test database.

To understand the basic concept, let us create a simple table Student in the test database and creates the few records in that table using command prompts as follows:

Step-1: Open the command prompt and change to the installation directory as follows:

  1. C:\Users\hpnmaratt>  
  2. C:\Users\hpnmaratt>cd C:\Program Files\MySQL\MySQL Server 5.7\bin  
  3. C:\Program Files\MySQL\MySQL Server 5.7\bin> 

Step-2: Login to the database using command prompt as shown below:

  1. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -u root -p  
  2. Enter password: ****  
  3. mysql> 

Step-3: Create the table Students in test database as shown below:

  1. mysql> use test;  
  2. mysql> create table Student  
  3.     (  
  4.      id int not null,  
  5.      First_Name varchar (255),  
  6.      Last_Name varchar (255),  
  7.      dob date  
  8.     );  
  9. Query OK, 0 rows affected (0.08 sec)  
  10. mysql> 

Step 4: In the final step, you need to create few data records in Students table as shown below:

  1. mysql> INSERT INTO student VALUES (150, 'Nakul', 'Jain', '1994/07/19' );  
  2. Query OK, 1 row affected (0.05 sec)  
  3.    
  4. mysql>  INSERT INTO Student VALUES (151, 'Ramesh', 'Kumar', '1992/03/9');  
  5. Query OK, 1 row affected (0.00 sec)  
  6.    
  7. mysql>  INSERT INTO Student VALUES (152, 'Ajeet', 'Singhal', '1992/01/23' );  
  8. Query OK, 1 row affected (0.00 sec)  
  9.    
  10. mysql> INSERT INTO Student VALUES (153, 'Hamza', 'Hussain', '1992/01/9');  
  11. Query OK, 1 row affected (0.00 sec)  
  12.    
  13. mysql> 

Let's see the simple JSP example to understand the use of <sql:dateParam> tag:

  1. <%@ page import="java.io.*,java.util.*,java.sql.*"%>  
  2. <%@ page import="javax.servlet.http.*,javax.servlet.*" %>  
  3. <%@ page import="java.util.Date,java.text.*" %>  
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  5. <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>  
  6.    
  7. <html>  
  8. <head>  
  9. <title>sql:dateParam Tag</title>  
  10. </head>  
  11. <body>  
  12.    
  13. <sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"  
  14.      url="jdbc:mysql://localhost/test"  
  15.      user="root"  password="1234"/>  
  16.   
  17. <%  
  18. Date DoB = new Date("2000/10/16");  
  19. int studentId = 151;  
  20. %>  
  21.    
  22. <sql:update dataSource="${db}" var="count">  
  23.    UPDATE Student SET dob = ? WHERE Id = ?  
  24.    <sql:dateParam value="<%=DoB%>" type="DATE" />  
  25.    <sql:param value="<%=studentId%>" />  
  26. </sql:update>  
  27.    
  28. <sql:query dataSource="${db}" var="rs">  
  29.    SELECT * from Student;  
  30. </sql:query>  
  31.    
  32. <table border="2" width="100%">  
  33. <tr>  
  34.    <th>Emp ID</th>  
  35.    <th>First Name</th>  
  36.    <th>Last Name</th>  
  37.    <th>DoB</th>  
  38. </tr>  
  39. <c:forEach var="table" items="${rs.rows}">  
  40. <tr>  
  41.    <td><c:out value="${table.id}"/></td>  
  42.    <td><c:out value="${table.First_Name}"/></td>  
  43.    <td><c:out value="${table.Last_Name}"/></td>  
  44.    <td><c:out value="${table.dob}"/></td>  
  45. </tr>  
  46. </c:forEach>  
  47. </table>  
  48.    
  49. </body>  
  50. </html>