`<sql:setDataSource> tag`

JSP 标准标签库JSP Standard Tag Library

The `<sql:setDataSource>` tag is used to configure a data source or store data source information in a variable within a certain scope, serving as the data source for other JSTL database operations.

Syntax format

<sql:setDataSource
  var="<string>"
  scope="<string>"
  dataSource="<string>"
  driver="<string>"
  url="<string>"
  user="<string>"
  password="<string>"/>

Attributes

The `<sql:setDataSource>` tag has the following attributes:
Attribute Description Required Default value
driver JDBC driver to register no None
url JDBC URL for the database connection no None
user Database username no None
password Database password no None
dataSource Pre-prepared database no None
var Variable representing the database no Default settings
scope Scope of the var attribute no Page


Example demonstration

Set up MySQL database:

  • Use the JDBC MySQL driver.
  • Connect to the local TEST database.
  • Use user_id and mypassword to access the TEST database.

The above parameters are very basic in MySQL and other databases; it is best to remember them. Next, here is a simple example of 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.


JSP 标准标签库JSP Standard Tag Library

Other extensions