In this tutorial you will learn about the JSTL sql update tag.
JSTL sql update tag
In this tutorial you will learn about the JSTL sql update tag.
<sql:update> tag of JSTL is used to execute the SQL update, these statements can be given either through the sql attribute or defined into its body.
Attributes of <sql:update> tag :
- var : This is an optional attribute that specifies the name of the variable or stores a value of the specified data source.
- scope : This is an optional attribute that specifies the scope of the variable.
- sql : This is an optional attribute that specifies the SQL update statements.
- dataSource : This is an optional attribute that specifies the database associated with it to update.
Example :
Here I am giving a simple example which will demonstrate you about the <sql:update> tag of JSTL. In this example at first I have created a table named student_info1(roll_no, name, date_of_birth). Then created a JSP page where I have used the <sql:update> tag and written the update query into its body to update the specific field value of the table.
Table student_info1
CREATE TABLE `student_info1` ( `roll_no` int(11) NOT NULL, `name` varchar(15) default NULL, `date_of_birth` date default NULL, PRIMARY KEY (`roll_no`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
JstlSqlUpdate.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.Date, java.text.Format, java.text.SimpleDateFormat" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>JSTL sql:update Example</title> </head> <body> <sql:setDataSource var="ds" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.10.13/data" user="root" password="root"/> <sql:query var="rs" dataSource="${ds}"> select name, date_of_birth from student_info1 </sql:query> <form> Student list : <table border="1"> <c:forEach var="rowValue" items="${rs.rows}"> <tr> <td>${rowValue.roll_no}</td> <td>${rowValue.name}</td> <td>${rowValue.date_of_birth}</td> </tr> </c:forEach> </table> </form> <b>Student List after update :</b> <sql:update dataSource="${ds}" var="upd"> update student_info1 set date_of_birth = '1985-02-02' where roll_no = 4 </sql:update> <sql:query var="rs" dataSource="${ds}"> select * from student_info1 </sql:query> <form> <table border="1"> <tr> <td><b>roll_no</b></td> <td><b>name</b></td> <td><b>date_of_birth</b></td> </tr> <c:forEach var="rowValue" items="${rs.rows}"> <tr> <td>${rowValue.roll_no}</td> <td>${rowValue.name}</td> <td>${rowValue.date_of_birth}</td> </tr> </c:forEach> </table> </form> </body> </html>
How to run this example
Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :
- jstl.jar
- standard.jar
After adding of these jar files you may execute your program in the following ways :
- Select JstlSqlUpdate.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- On the Eclipse Editor go to your JstlSqlUpdate.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
- A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish
NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.
Output :
1. student_info1 table
2. When you will execute the above JSP page you will get the output on your eclipse browser as follows :
[ 0 ] Comments