JSTL SQL sql:setDataSource Tag

The <sql:setDataSource> tag is used to create data source variable directly from JSP and it can be stored in a scoped variable with the help of scoped attribute. It can be used as input to other database actions.

Attributes of <sql:setDataSource> Tag

  • datasource: It specify data source name or java.sql.DataSource object.
  • driver: It specifies JDBC driver class name used to create a connection.
  • url: It specifies JDBC URL associated with database.
  • user: It specifies database username.
  • password: It specifies database password.
  • var: It specifies name of the variable which is used to store created data source object.
  • scope: It specifies scope of the variable like page or session or request or application.

Example

<%@ 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>SQL:setDataSource Tag</title>
</head>
<body>
	<!-- Connect to the database -->
	<sql: setDataSource var="ds" driver="com.mysql.jdbc.Driver"
		url="jdbc: mysql: //localhost/test" user="root" password="" />

The data is below:
	<br>

	<!--  Execute the SQL -->
	<sql:query dataSource="${ds}"
		sql="select * from employees where id >=100" var="result" />

	<!-- How may rows are returned -->
	<c:out value="The rows returned by SQL : ${result.getRowCount()}" />

</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.
    • <sql:query dataSource=”${ds}” sql=”select * from employees where id >=100″ var=”result” /> tag is used to specify SQL statement to be run on the database and stores result of SQL statement in the variable.
    • <c:out value=”The rows returned by SQL : ${result.getRowCount()}” /> tag is used to display the result of SQL statement in the output using getRowCount() method which indiacates number of rows returned by the SQL statement.

Steps for Execution

  • Save the file as setDataSourceExample.jsp in eclipse IDE.
  • Now select the jsp file, right click on mouse and select Run as -> Run on Server.

Output

After successful execution of the program we will get the following result:
setDataSource

Previous Tutorial : JSTL SQL sql:transaction  Tag :: Next Tutorial : JSTL Functions

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*