<sql:setDataSource> Tag
The <sql:setDataSource>
tag is used to configure a data source or store data source information in a variable within a specified scope, which serves as the data source for other JSTL database operations.
Syntax
<sql:setDataSource
var="<string>"
scope="<string>"
dataSource="<string>"
driver="<string>"
url="<string>"
user="<string>"
password="<string>"/>
Attributes
Attribute | Description | Required | Default Value |
---|---|---|---|
driver | The JDBC driver to be registered | No | None |
url | The JDBC URL for the database connection | No | None |
user | The database username | No | None |
password | The database password | No | None |
dataSource | A pre-prepared database | No | None |
var | The variable representing the database | No | Default setting |
scope | The scope of the var attribute | No | Page |
Example Demonstration
Setting up a MySQL Database:
- Using the JDBC MySQL driver.
- Connecting to the local TEST database.
- Accessing the TEST database with user_id and mypassword.
These parameters are very basic in MySQL or other databases and it's best to remember them. Below is a simple example using the <sql:setDataSource>
tag:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
<title>JSTL sql:setDataSource Tag</title>
</head>
<body>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/TEST"
user="user_id" password="mypassword"/>
<sql:query dataSource="${snapshot}" sql="..." var="result" />
</body>
</html>
You will use the <sql:setDataSource> tag in other SQL tags.