<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">JSTL Query Tag provides capability to fetch the data from databased by executing query directly from JSP and it can be stored in a variable to use later with the help of scope attribute.<br />
<b><br />
</b> <b>JSTL SQL Query Tag has following attributes.</b></p>
<p> <b>1. datasource Attribute:</b> Specifies the datasource. Make sure that you already created datasource using serDataSource tag before writing query.</p>
<p> <b>2. sql Attribute:</b> Specifies the SQL statement to run on database.</p>
<p> <b>3. var Attribute: </b>Store the result of SQL statement.</p>
<p> Syntax-<br />
<;sql:query</p>
<p>  ; var=&#8221;<;string>;&#8221;</p>
<p>  ; scope=&#8221;<;string>;&#8221;</p>
<p>  ; sql=&#8221;<;string>;&#8221;</p>
<p>  ; dataSource=&#8221;<;string>;&#8221;</p>
<p>  ; startRow=&#8221;<;string>;&#8221;</p>
<p>  ; maxRows=&#8221;<;string>;&#8221;/>; </p>
<p>
 <b>JSP SQL <i><;sql:query>;</i> tag Example:</b></p>
<pre class="highlight" name="code"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>;
<;%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>;

<;!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 Tags - setDataSource Example<;/title>;
<;/head>;
<;body>;

 <;sql:setDataSource driver="com.mysql.jdbc.Driver"
 url="jdbc:mysql://localhost:3306/database_name"
 var="localSource" 
 user="database_user" 
 password="database_password"/>;

 <;sql:query dataSource="${localSource}" var="result">;
SELECT * from Employees;
<;/sql:query>;
 
<;table border="1" width="100%">;
<;tr>;
<;th>;Emp ID<;/th>;
<;th>;First Name<;/th>;
<;th>;Last Name<;/th>;
<;th>;Age<;/th>;
<;/tr>;
<;c:forEach var="row" items="${result.rows}">;
<;tr>;
<;td>;<;c:out value="${row.id}"/>;<;/td>;
<;td>;<;c:out value="${row.first}"/>;<;/td>;
<;td>;<;c:out value="${row.last}"/>;<;/td>;
<;td>;<;c:out value="${row.age}"/>;<;/td>;
<;/tr>;
<;/c:forEach>;
<;/table>;
<;/body>;
<;/html>;
</pre>
<p><b><br />
</b> <b>Now try to access above JSP, which should display the following result:</b></p>
<table border="1" style="width: 100%px;">
<tbody>
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<tr>
<td>1111</td>
<td>Dinesh</td>
<td>Rajput</td>
<td>27</td>
</tr>
<tr>
<td>2222</td>
<td>Sweety</td>
<td>Rajput</td>
<td>24</td>
</tr>
</tbody>
</table>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/10/jstl-sql-setdatasource-example.html">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/2013/10/jsp-standard-tag-library-jstl-tutorial.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/10/jstl-update-tag-example.html">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/jstl-sql-setdatasource-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/jstl-update-tag-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '297'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…