ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How to use JSTL to fetch data from database?

user-image
تم إضافة السؤال من قبل Mohd Nadeem Mohd Rehman Mohammed , Software Engineer , Softworx Technologies
تاريخ النشر: 2016/09/16
Mohd Nadeem Mohd Rehman Mohammed
من قبل 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>

 

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟