Querying Data with the JSTL in JSP

By: Sathya Narayana Emailed: 1675 times Printed: 2153 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

To query data with the JSTL, follow these steps:

  1. Create a new Web application folder called DataAccess inside Tomcat's webapps folder. Create a folder inside it named WEB-INF.

  2. Next you need to give your Web application access to the JSTL by copying the JSTL lib folder into WEB-INF. Be sure to copy the entire lib folder, not just its contents.

  3. You also need to make the MySQL JDBC classes available, which you can find at http://dev.mysql.com/downloads/connector/j/. Once you've downloaded the file, unzip it and copy mysql-connector-java-3.0.9-bin.jar into your webapps folder's WEB-INF/lib directory (the filename may be slightly different if a newer version has been released).

  4. Create the following JSP page in the DataAccess folder as bookList.jsp:

    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
    <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql_rt" %>
    
    <sql:setDataSource var="datasource"
                     driver="com.mysql.jdbc.Driver"
                        url="jdbc:mysql://localhost/publish"
                       user="publishuser" password="secret"/>
    
    <sql:query var="books" dataSource="${datasource}">
      SELECT id, title, price FROM book
    </sql:query>
    <html>
      <head>
        <title>A First JSP Database</title
      </head>
      <body>
        <table border="1">
          <tr>
             <td>id</td><td>title</td><td>price</td>
          </tr>
    <c:forEach items="${books.rows}" var="row">
          <tr>
            <td><c:out value="${row.id}" /></td>
            <td><c:out value="${row.title}" /></td>
            <td><c:out value="${row.price}" /></td>
          </tr>
    </c:forEach>
       </table>
      </body>
    </html>
    
  5. Start Tomcat.

  6. Start a Web browser, and navigate to http://localhost:8080/DataAccess/bookList.jsp.


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Sathya Narayana
Querying Data with the JSTL in JSP
Deleting Data in MySQL
Modifying data and using WHERE clause in MySQL
Querying the Database in MySQL
Inserting Data into Tables in MySQL
Changing the Structure of an Existing Table in MySQL
The Exception Object in JSP
The Application Object in JSP
The Session Object in JSP
The Out Object in JSP
The Response Object in JSP
The Request Object in JSP
Tag Libraries in JSP
Scriptlets and Expressions in JSP
Declarations in JSP

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
What are the different scopes in JSP?
JSP Example to connect to MS SQL database and retrieve records
Automatically Refreshing a JSP
JSP CheckBox Example
Sending Email using JSP
JSP Alert Example
JSP Program for display Date
Comparison operators in JSP
Getting Started with JSP
Retrieving a Portion of a String
Arithmetic Evaluation Using the Expression Language in JSP
Syntax For JSP Declaratives
Tags using in jsp
The Advantages of JSP
Checkbox Object
Most Emailed Articles (in JSP)
Textarea Object
Click to Activate and Use this control
The Servlet Life Cycle
JSP Example to connect to MS SQL database and retrieve records
A JSP page that gets properties from a bean
The JSP Program running first Time.
Embedding java codes in jsp sciptlets
Debugging Servlets
Document Object
Password Object
What is JSP?
A Servlet That Generates HTML
The Advantages of JSP
Retrieving a Portion of a String
Working with Status Bar Messages