<sql: param> tag is used to set parameter values for SQL statement. This tag uses action of both <sql: query> and <sql: update> to provide value for value placeholder.
Attribute of <sql: param> tag
- value: It is used to specify value to the parameter.
Example of <sql: param> tag
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <sql:setDataSource var="ds" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/test" user="root" password="" /> <c:set var="id" value="202"/> <sql:update dataSource="${ds}" var="count"> UPDATE student SET name="maruti" WHERE id=? <sql:param value="${id}"/> </sql:update> <sql:query dataSource="${ds}" var="result"> SELECT * from student; </sql:query> <table border="1" width="300"> <tr> <th>ID</th> <th>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.name}" /></td> <td><c:out value="${row.dob}" /></td> </tr> </c:forEach> </table> </body> </html>
Details of the Code
- <sql: setDataSource var=”ds” driver=”com.mysql.jdbc.Driver” url=”jdbc: mysql: //localhost/test” user=”root” password=”” /> tag is used to connect to the data base and specify variable name for data source and driver name, url , user name and password of the database.
- <c:set var=”id” value=”202″/> tag is used to set value of the variable in the given scope.
- <sql:param value=”${id}”/> tag is used to provide value of the parameter using value attribute.
- <sql:query dataSource=”${ds}” var=”result”> SELECT * from student; </sql:query> tag is used to fetch the data from the database table which takes attributes such as ,datasource attribute specifies the datasource, var attribute result of the SQL statement.
- <c:forEach var=”row” items=”${result.rows}”> tag is used to produce database table from SQL query and items attribute specifies collection of items to iterate in the loop.
Steps for Execution
- Save the file as ParamExample.jsp in eclipse IDE.
- Now select the jsp file, right click on mouse and select Run as -> Run on Server.
Output
Before execution of the program table would be like this:
After successful execution of the program we will get the following result:
Previous Tutorial : JSTL SQL sql:update Tag :: Next Tutorial : JSTL SQL sql:dateParam Tag