JSTL Sql setDataSource Tag : Jstl provides the facility to deal with database. JSTL has the separate sql tag library which provides the tags to do database operations.
JSTL provides the <sql:setDataSource> tag which is used to create the database connection. JSTL setDataSource tag create the connection with database and holds that connection in the local variable to use it later with the help of scope attribute.
JSTL setDataSource Tag has following attributes :
- driver Attribute: Specifies the driver to connect database. In this example, we are using MySQL database so driver is com.mysql.jdbc.Driver
- url Attribute: Define the location of the database.
- var Attribute: Define the variable which holds the dataSource once it is created.
- user Attribute: Define the username/user to access database.
- password Attribute: Define the password for the username/user to access database.
- scope Attribute : Scope of the variable
Example of JSTL Sql <sql:setdatasource> Tag :</sql:setdatasource>
Home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<!DOCTYPE html>
<html>
<head>
<title>JSTL SQL Tags</title>
</head>
<body>
<sql:setDataSource driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/databaseNname"
var="dataSource"
user="dbuser"
password="dbpassword"
scope="session"/>
<sql:query dataSource="${dataSource}"
sql="SELECT * FROM userInfoTable WHERE ustatus ='Active' "
var="result" />
<table>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.uid}"/></td>
<td><c:out value="${row.uname}"/></td>
<td><c:out value="${row.uage}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
0 Comment(s)