
- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Database
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
How to execute DELETE SQL in a JSP?
The <sql:update> tag executes an SQL statement that does not return data; for example, SQL INSERT, UPDATE, or DELETE statements.
Attribute
The <sql:update> tag has the following attributes −
Attribute | Description | Required | Default |
---|---|---|---|
sql | SQL command to execute (should not return a ResultSet) | No | Body |
dataSource | Database connection to use (overrides the default) | No | Default database |
var | Name of the variable to store the count of affected rows | No | None |
scope | Scope of the variable to store the count of affected rows | No | Page |
Example
To start with basic concept, let us create a simple table Employees table in the TEST database and create few records in that table as follows −
Step 1
Open a Command Prompt and change to the installation directory as follows −
C:\> C:\>cd Program Files\MySQL\bin C:\Program Files\MySQL\bin>
Step 2
Login to the database as follows −
C:\Program Files\MySQL\bin>mysql -u root -p Enter password: ******** mysql>
Step 3
Create the table Employee in the TEST database as follows −
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>
Create Data Records
We will now create few records in the Employee table as follows −
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>
Let us now write a JSP which will make use of the <sql:update> tag to execute an SQL DELETE statement to update one record in the table as follows −
<%@ 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"> Delete from Employees where id = 101; </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>
Access the above JSP, the following result will be displayed −
+-------------+----------------+-----------------+-----------------+ | Emp ID | First Name | Last Name | Age | +-------------+----------------+-----------------+-----------------+ | 100 | Zara | Ali | 18 | | 102 | Zaid | Khan | 30 | | 103 | Sumit | Mittal | 28 | | 104 | Nula | Ali | 2 | +-------------+----------------+-----------------+-----------------+
Similar way, you can try SQL UPDATE and INSERT statements on the same table.
- Related Articles
- How to execute UPDATE SQL in a JSP?
- How to execute SQL update query in a JSP?
- How to use parameterized SQL query in a JSP?
- How to delete Session data in JSP?
- How to executes an SQL SELECT statement in a JSP?
- How to delete cookies with JSP?
- How to pass a date variable in sql query in a JSP?
- How can you delete a session data in JSP?
- Difference Between DELETE and DROP in SQL
- Difference between DELETE and DROP SQL
- Difference between Delete and truncate in sql query
- Delete duplicate alphanumeric entries from column data in SQL
- How to use a filter in JSP?
- I want to create a custom tag in JSP. How to write a custom tag in JSP?
- How to Execute a Strategic Planning?
