Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • JSTL Sql <sql:setDataSource> Tag

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 878
    Comment on it

    JSTL Sql setDataSource Tag : Jstl provides the facility to deal with database. JSTL has the separate sql tag library which provides the tags to do database operations. JSTL provides the <sql:setDataSource> tag which is used to create the database connection. JSTL setDataSource tag create the connection with database and holds that connection in the local variable to use it later with the help of scope attribute.

    JSTL setDataSource Tag has following attributes :

    1. driver Attribute: Specifies the driver to connect database. In this example, we are using MySQL database so driver is com.mysql.jdbc.Driver
    2. url Attribute: Define the location of the database.
    3. var Attribute: Define the variable which holds the dataSource once it is created.
    4. user Attribute: Define the username/user to access database.
    5. password Attribute: Define the password for the username/user to access database.
    6. scope Attribute : Scope of the variable

    Example of JSTL Sql <sql:setdatasource> Tag :</sql:setdatasource>

    Home.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
    <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
    
    <!DOCTYPE html>
    <html>
        <head>
            <title>JSTL SQL Tags</title>
        </head>
        <body>
                <sql:setDataSource driver="com.mysql.jdbc.Driver"
                                       url="jdbc:mysql://localhost:3306/databaseNname"
                                       var="dataSource" 
                                       user="dbuser"  
                                       password="dbpassword"
                scope="session"/>
    
                <sql:query dataSource="${dataSource}" 
                           sql="SELECT * FROM userInfoTable WHERE ustatus ='Active' " 
                           var="result" />
            <table>
            <c:forEach var="row" items="${result.rows}">
              <tr>
                <td><c:out value="${row.uid}"/></td>
                <td><c:out value="${row.uname}"/></td>
                <td><c:out value="${row.uage}"/></td>  
              </tr>
            </c:forEach>
            </table>
        </body>
    </html>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: