In this tutorial you will learn about the JSTL sql param tag.
JSTL sql param Tag
In this tutorial you will learn about the JSTL sql param tag.
JSTL <sql:param> tag is used to give the value as a parameter in SQL statements.
Attributes of <sql:param> tag :
- value : This is an optional attribute that is used to give the value to the parameter.
Example :
An example is being given below will demonstrate you about the use of <sql:param> tag. n this example at first I have created a table named student_info1 into which inserted some values in their respective fields. Then created a JSP page where used the <sql:setDataSource> tag to create data source next I have designed a form to take the input roll no. of student by the user then checked for the null value of the text field and used the <sql:query> tag to execute the select query by the roll no. Then displayed the detail of the student by roll no.
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
JstlSqlParam.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:param 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"/> <form> <p>Enter Roll no. of Student : <input type="text" name="roll"/> <p><input type="submit" value="submit"/> </form> <% if(request.getParameter("roll")!= null) { %> <sql:query var="rs" dataSource="${ds}"> select * from student_info1 where roll_no = ? <sql:param value="${param.roll}" /> </sql:query> <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> <% } %> </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 JstlSqlParam.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 JstlSqlParam.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 :
3. When you will input the value and when click on submit button you will get the output as follows :
[ 0 ] Comments