How to set the data source in a JSP?

JSPJava 8Object Oriented ProgrammingProgramming

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.


The <sql:setDataSource> tag has the following attributes −

driverName of the JDBC driver class to be registeredNoNone
urlJDBC URL for the database connectionNoNone
userDatabase usernameNoNone
passwordDatabase passwordNoNone
passwordDatabase passwordNoNone
dataSourceDatabase prepared in advanceNoNone
varName of the variable to represent the databaseNoSet default
scopeScope of the variable to represent the databaseNoPage


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 = "" prefix = "c" %>
<%@ taglib uri = "" prefix = "sql"%>
      <title>JSTL sql:setDataSource Tag</title>
      <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" />

We will use <sql:setDataSource> in subsequent SQL tags.

Published on 25-Mar-2019 10:17:10