- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set the data source in a JSP?
The <sql:setDataSource> tag sets the data source configuration variable or saves the data-source information in a scoped variable that can be used as input to the other JSTL database actions.
Attribute
The <sql:setDataSource> tag has the following attributes −
Attribute | Description | Required | Default |
---|---|---|---|
driver | Name of the JDBC driver class to be registered | No | None |
url | JDBC URL for the database connection | No | None |
user | Database username | No | None |
password | Database password | No | None |
password | Database password | No | None |
dataSource | Database prepared in advance | No | None |
var | Name of the variable to represent the database | No | Set default |
scope | Scope of the variable to represent the database | No | Page |
Example
Consider the following information about your MySQL database setup −
We are using JDBC MySQL driver.
We are going to connect to TEST database on local machine.
We would use user_id and my password to access TEST database.
All the above parameters will vary based on your MySQL or any other database setup. Considering the above parameters, the following example uses the setDataSource tag −
<%@ 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>
We will use <sql:setDataSource> in subsequent SQL tags.
- Related Articles
- How to set time zone in a JSP?
- How to delete Session data in JSP?
- How do you set cookies in the JSP?
- How to read form data using JSP?
- How to set result of a java expression in a property in JSP?
- How can you delete a session data in JSP?
- How to pass check boxes data using JSP?
- How to set locate to identify required resource bundle in JSP?
- How to read form data using JSP via GET Method?
- How to read form data using JSP via POST Method?
- How to convert a Kotlin source file to a Java source file?
- What happens when buffer is set to a value "none" in JSP?
- How to specify the encoding type used by forms that post data back to the Web application in a JSP?
- How to use a filter in JSP?
- I want to create a custom tag in JSP. How to write a custom tag in JSP?

Advertisements