JSTL SQL Tag sql:setDataSource Example
- Details
- Written by Nam Ha Minh
- Last Updated on 31 August 2019   |   Print Email
This post helps you understand how to use the <sql:setDataSource> tag in the JSTL SQL tags library with code example.
You know, the <sql:setDataSource> creates a datasource to connect to a database. A datasource creates a way to communicate with the database server. We use datasource to query, insert, update or delete information from the databases.
Apparently, we should separate the database specific logic from the presentation logic (as defined in the MVC). Since we’ll have to use this tag in JSP, we need to define database logic in the JSP which is used for presentation. However, this rule can be eased in simple cases like prototyping or developing a proof of concept for simplicity.
JSTL <sql:setDataSource> Syntax:
<sql:setDataSource
var="<string>"
scope="<string>"
dataSource="<string>"
driver="<string>"
url="<string>"
user="<string>"
password="<string>"/>
Attributes:
Name | Required | Description |
var | False | Name of the datasource configuration variable. |
scope | False | Scope of the datasource configuration variable. |
dataSource | False | Datasource relative path in the JNDI tree. |
driver | False | JDBC Driver class name. |
url | False | JDBC connection url of the database to connect. |
user | False | Database user name to connect. |
password | False | Database password to connect. |
JSTL <sql:setDataSource> Example:
The following JSP code establishes a database connection with a MySQL server. Please note that we cannot do much with a datasource alone, we need to use other relevant SQL tags to interact with database. In order to make sure that the datasource has been setup successfully, we are printing the variable which stores datasource.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="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><sql:setDataSource> Demo</title> </head> <body> <h1><sql:setDataSource> Demo</h1> <sql:setDataSource var="myDS" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" user="root" password=""/> <c:out value="${myDS}"/> </body> </html>
Output:
In the above screen capture, we can clearly see that the datasource has been successfully created.
Recommended Usage of <sql:setDataSource> tag:
Having database specific logic in the JSPs (in the presentation layer to be precise) is not a best practice in modern web application development. However, we can take little liberty to do so, provided the application is intended for prototyping or proof of concept (POC).
JDBC Driver Download:
In order to successfully create datasource we needed to have JDBC Driver JAR file. We can download the archive from the website http://dev.mysql.com/downloads/connector/j/5.0.html . Once we download the latest driver, we need to place the JAR file named mysql-connector-java-x.x.x-bin.jar under WEB-INF/lib folder.
Other JSTL SQL Tags:
dateParam | param | query | transaction | update
Comments
putting the driver jar into WEB-INF/lib doesn't helped against driver not found (HTTP Status 500) at least not on tomcat with Netbeans with jstl (jakarta) Tag. Strangely in my case a normal servlet with exactly the same query works in the same war without problems...
bye B.M.
Here you go: How to list records in a database table using JSP and JSTL