JSTL SQL sql:query Tag

JSTL sql:query Tag for beginners and professionals with examples on JSTL core tags, function tags, formatting tags, SQL tags, and miscellaneous tags.

JSTL SQL sql:query Tag

JSTL SQL sql:query Tag

The tag is used for executing the SQL query defined in its sql attribute or the body. It is used to execute an SQL SELECT statement and saves the result in scoped variable.

Example:

  1. <sql:query dataSource="${db}" var="rs">  
  2. SELECT * from Students;  
  3. sql:query>  

JSTL SQL Complete Example

Consider the below information about your MySQL database setup:

  • We are using the JDBC MySQL driver
  • We are using the test database on local machine
  • We are using the "root" as username and "1234" as password to access the test database.

To understand the basic concept, let us create a simple table Students in the test database and creates the few records in that table using command prompts as follows:

Step-1: Open the command prompt and change to the installation directory as follows:

  1. C:\Users\hpnmaratt>  
  2. C:\Users\hpnmaratt>cd C:\Program Files\MySQL\MySQL Server 5.7\bin  
  3. C:\Program Files\MySQL\MySQL Server 5.7\bin>  

It will look like this:

Step-2: Login to the database using command prompt as shown below:

  1. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -u root -p  
  2. Enter password: ****  
  3. mysql>  

It will look like this:

Step-3: Create the table Students in test database as shown below:

  1. mysql> use test;  
  2. mysql> create table Students  
  3.     (  
  4.      id int not null,  
  5.      First_Name varchar (255),  
  6.      Last_Name varchar (255),  
  7.      Age int not null  
  8.     );  
  9. Query OK, 0 rows affected (0.08 sec)  
  10. mysql>  

It will look like this:

Step 4: In final step you need to create few data records in Students table as shown below:

  1. mysql> INSERT INTO Students VALUES (150'Nakul''Jain'22);  
  2. Query OK, 1 row affected (0.05 sec)  
  3.    
  4. mysql>  INSERT INTO Students VALUES (151'Ramesh''Kumar'20);  
  5. Query OK, 1 row affected (0.00 sec)  
  6.    
  7. mysql>  INSERT INTO Students VALUES (152'Ajeet''Singhal'22);  
  8. Query OK, 1 row affected (0.00 sec)  
  9.    
  10. mysql> INSERT INTO Students VALUES (153'Hamza''Hussain'22);  
  11. Query OK, 1 row affected (0.00 sec)  
  12.    
  13. mysql>  

It will look like this:

Let's see the simple JSP example to understand the use of tag is:

  1. <%@ page import="java.io.*,java.util.*,java.sql.*"%>  
  2. <%@ page import="javax.servlet.http.*,javax.servlet.*" %>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>  
  5.   
  6. <html>  
  7. <head>  
  8. <title>sql:query Tagtitle>  
  9. head>  
  10. <body>  
  11.    
  12. <sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"  
  13.      url="jdbc:mysql://localhost/test"  
  14.      user="root"  password="1234"/>  
  15.   
  16. <sql:query dataSource="${db}" var="rs">  
  17. SELECT * from Students;  
  18. sql:query>  
  19.    
  20. <table border="2" width="100%">  
  21. <tr>  
  22. <th>Student IDth>  
  23. <th>First Nameth>  
  24. <th>Last Nameth>  
  25. <th>Ageth>  
  26. tr>  
  27. <c:forEach var="table" items="${rs.rows}">  
  28. <tr>  
  29. <td><c:out value="${table.id}"/>td>  
  30. <td><c:out value="${table.First_Name}"/>td>  
  31. <td><c:out value="${table.Last_Name}"/>td>  
  32. <td><c:out value="${table.Age}"/>td>  
  33. tr>  
  34. c:forEach>  
  35. table>  
  36.   
  37. body>  
  38. html>