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.
<sql:setDataSource
var="<string>"
scope="<string>"
dataSource="<string>"
driver="<string>"
url="<string>"
user="<string>"
password="<string>"/>
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. |
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>
In the above screen capture, we can clearly see that the datasource has been successfully created.
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).
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.
dateParam | param | query | transaction | update