JSTL - Tag SQL <sql: update>

Il <sql:update>tag esegue un'istruzione SQL che non restituisce dati; per esempio,SQL INSERT, UPDATE, o DELETE dichiarazioni.

Attributo

Il <sql:update> tag ha i seguenti attributi:

Attributo Descrizione necessario Predefinito
sql Comando SQL da eseguire (non dovrebbe restituire un ResultSet) No Corpo
fonte di dati Connessione al database da utilizzare (sostituisce l'impostazione predefinita) No Database predefinito
var Nome della variabile per memorizzare il conteggio delle righe interessate No Nessuna
scopo Ambito della variabile per memorizzare il conteggio delle righe interessate No Pagina

Esempio

Per iniziare con il concetto di base, creiamo una semplice tabella Employees tabella nel database TEST e creare pochi record in quella tabella come segue:

Passo 1

Apri un file Command Prompt e passare alla directory di installazione come segue:

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

Passo 2

Accedi al database come segue:

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

Passaggio 3

Crea la tabella Employee nel TEST database come segue -

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

Crea record di dati

Creeremo ora pochi record nel file Employee tabella come segue -

mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
Query OK, 1 row affected (0.05 sec)
 
mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');
Query OK, 1 row affected (0.00 sec)
 
mysql>

Scriviamo ora un JSP che utilizzerà l'estensione <sql:update> tag per eseguire un SQL INSERT istruzione per creare un record nella tabella come segue:

<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ 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:update Tag</title>
   </head>

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

      <sql:update dataSource = "${snapshot}" var = "count"> INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali'); </sql:update> <sql:query dataSource = "${snapshot}" var = "result">
         SELECT * from Employees;
      </sql:query>
 
      <table border = "1" width = "100%">
         <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</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.age}"/></td>
            </tr>
         </c:forEach>
      </table>

   </body>
</html>

Accedi al JSP sopra, verrà visualizzato il seguente risultato:

+-------------+----------------+-----------------+-----------------+
|    Emp ID   |    First Name  |     Last Name   |       Age       |
+-------------+----------------+-----------------+-----------------+
|     100     |    Zara        |     Ali         |       18        |
|     101     |    Mahnaz      |     Fatma       |       25        |
|     102     |    Zaid        |     Khan        |       30        |
|     103     |    Sumit       |     Mittal      |       28        |
|     104     |    Nula        |     Ali         |       2         |
+-------------+----------------+-----------------+-----------------+

In modo simile, puoi provare SQL UPDATE e DELETE dichiarazioni sullo stesso tavolo.