JSTL SQL sql:setDataSource Tag

JSTL sql:setDataSource Tag for beginners and professionals with examples on JSTL core tags, function tags, formatting tags, SQL tags, and miscellaneous tags.

JSTL SQL sql:setDataSource Tag

JSTL SQL sql:setDataSource Tag

The <sql:setDataSource> tag is used for creating a simple data source suitable only for prototyping.

It is used to create the data source variable directly from JSP and it is stored inside a scoped variable. It can be used as input for other database actions.

Example:

Consider the below information about your MySQL database setup:

  • We are using the JDBC MySQL driver
  • We are using the test database on local machine
  • We are using the "root" as username and "1234" as password to access the test database.

Let's see the simple example to understand the xml <sql:setDataSource> tag is:

  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>  
  3. <html>  
  4. <head>  
  5. <title>sql:setDataSource Tag</title>  
  6. </head>  
  7. <body>  
  8.    
  9. <sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"  
  10.      url="jdbc:mysql://localhost/test"  
  11.      user="root"  password="1234"/>  
  12. </body>  
  13. </html>  

The above code is used as input for other database actions. It is used for setting the connection with database server. Therefore you will start using the <sql:setDataSource> in subsequent SQL tags.