JSTL - SQL <sql:transaction> 标签



<sql:transaction> 标签用于将 <sql:query><sql:update> 标签组合成事务。您可以将任意数量的 <sql:query><sql:update> 标签作为语句放在 <sql:transaction> 标签内以创建一个单一事务。

它确保嵌套操作执行的数据库修改要么提交,要么在任何嵌套操作抛出异常时回滚。

属性

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

属性 描述 必填 默认值
dataSource 要使用的数据库连接(覆盖默认值) 默认数据库
isolation 事务隔离级别 (READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, 或 SERIALIZABLE) 数据库默认值

示例

首先,让我们在 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:transaction>标签来执行SQL UPDATE语句。此处,<sql:transaction>标签内的代码要么完全执行,要么根本不执行:

<%@ 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:transaction Tag</title>
   </head>

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

      <%
         Date DoB = new Date("2001/12/16");
         int studentId = 100;
      %>

      <sql:transaction dataSource = "${snapshot}">
         <sql:update var = "count">
            UPDATE Students SET last = 'Ali' WHERE Id = 102
         </sql:update>
         
         <sql:update var = "count">
            UPDATE Students SET last = 'Shah' WHERE Id = 103
         </sql:update>
         
         <sql:update var = "count">
            INSERT INTO Students 
            VALUES (104,'Nuha', 'Ali', '2010/05/26');
         </sql:update>
      </sql:transaction>

      <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,将显示以下结果:

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