JSTL - SQL <sql:dateParam> 标签



<sql:dateParam> 标签用作 <sql:query><sql:update> 标签的嵌套操作,用于为值占位符提供日期和时间值。如果提供空值,则将值设置为占位符的 SQL NULL

属性

<sql:dateParam> 标签具有以下属性:

属性 描述 必填 默认值
要设置的日期参数的值 (java.util.Date) 主体
类型 DATE(仅日期)、TIME(仅时间)或 TIMESTAMP(日期和时间) TIMESTAMP

示例

为了开始基本概念,让我们在 TEST 数据库中创建一个简单的表 Students 表,并在该表中创建一些记录,如下所示:

步骤 1

打开 命令提示符 并更改为安装目录,如下所示:

C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>

步骤 2

登录到数据库,如下所示:

C:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ********
mysql>

步骤 3

TEST 数据库中创建 Employee 表,如下所示:

mysql> use TEST;
mysql> create table Students
   (
      id int not null,
      first varchar (255),
      last varchar (255),
      dob date
   );
Query OK, 0 rows affected (0.08 sec)
mysql>

创建数据记录

现在,我们将在 Employee 表中创建一些记录,如下所示:

mysql> INSERT INTO Students 
   VALUES (100, 'Zara', 'Ali', '2002/05/16');
Query OK, 1 row affected (0.05 sec)
 
mysql> INSERT INTO Students 
   VALUES (101, 'Mahnaz', 'Fatma', '1978/11/28');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO Students 
   VALUES (102, 'Zaid', 'Khan', '1980/10/10');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO Students 
   VALUES (103, 'Sumit', 'Mittal', '1971/05/08');
Query OK, 1 row affected (0.00 sec)
 
mysql>

现在让我们编写一个 JSP,它将使用 <sql:update> 标签以及 <sql:param> 标签和 <sql:dataParam> 标签来执行 SQL UPDATE 语句以更新 Zara 的出生日期:

<%@ 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"%>
 
<html>
   <head>
      <title>JSTL sql:dataParam Tag</title>
   </head>

   <body>
      <sql:setDataSource var = "snapshot" driver = "com.mysql.jdbc.Driver"
         url = "jdbc:mysql://127.0.0.1/TEST" user = "root" password = "pass123"/>

      <%
         Date DoB = new Date("2001/12/16");
         int studentId = 100;
      %>
 
      <sql:update dataSource = "${snapshot}" var = "count">
         UPDATE Students SET dob = ? WHERE Id = ?
         <sql:dateParam value = "<%=DoB%>" type = "DATE" />
         <sql:param value = "<%=studentId%>" />
      </sql:update>
 
      <sql:query dataSource = "${snapshot}" var = "result">
         SELECT * from Students;
      </sql:query>
 
      <table border = "1" width="100%">
         <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last 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.first}"/></td>
               <td> <c:out value = "${row.last}"/></td>
               <td> <c:out value = "${row.dob}"/></td>
            </tr>
         </c:forEach>
      </table>
 
   </body>
</html>

访问上面的 JSP,将显示以下结果。将 ID = 100 的记录的 dob 从 2002/05/16 更新到 2001/12/16:

+-------------+----------------+-----------------+-----------------+
|    Emp ID   |    First Name  |     Last Name   |       DoB       |
+-------------+----------------+-----------------+-----------------+
|     100     |    Zara        |     Ali         |   2001-12-16    |
|     101     |    Mahnaz      |     Fatma       |   1978-11-28    |
|     102     |    Zaid        |     Khan        |   1980-10-10    |
|     103     |    Sumit       |     Mittal      |   1971-05-08    |
+-------------+----------------+-----------------+-----------------+
jsp_standard_tag_library.htm
广告