Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How to use JSTL to fetch data from database?

user-image
Question ajoutée par Mohd Nadeem Mohd Rehman Mohammed , Software Engineer , Softworx Technologies
Date de publication: 2016/09/16
Mohd Nadeem Mohd Rehman Mohammed
par Mohd Nadeem Mohd Rehman Mohammed , Software Engineer , Softworx Technologies

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. JSTLhas support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> 

 

define datasource inside web.xml so all jsp files access that database easily

web.xml

<context-param>

    <param-name>javax.servlet.jsp.jstl.sql.dataSource</param-name>

    <param-value>jdbc:mysql://localhost:3306/dbname,

  com.mysql.jdbc.Driver,username,password  </param-value>

 

  </context-param>

 

showData.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>    

<!DOCTYPE html>

<html>

<body>

 

<sql:query>select Message from test<sql:query>

 

<p>Inside query tag you have to write query and parameter if you want.

and access you resultset using forEach loop </p>

 

<c:forEach var="row" items="${result.rows }">

<c:out value="${row.Message }"></c:out>

</c:forEach>

 

</body>

</html>

 

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?