1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ 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 lang="CN">
<head>
<meta charset="UTF-8">
<title>Welcome to Page</title>
</head>
<body>
<!--
JDBC驱动、URL 数据库的用户名、密码
useUnicode=true&characterEncoding=utf-8 防止中文乱码
-->
<sql:setDataSource var="sqlDataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8"
user="root" password="123456"/>



<!-- 1.列表(产品名称+产品链接) -->
<ul>
<sql:query dataSource="${sqlDataSource}" var="productList">
SELECT * FROM product;
</sql:query>
<c:forEach var="item" items="${productList.rows}">
<li>
<a href="${item.product_url}"> <c:out value="${item.product_name}"/> </a>
</li>
</c:forEach>
</ul>



<!-- 2.多表联查列表(产品名称+产品链接+产品图片) -->
<ul class="">
<sql:query dataSource="${sqlDataSource}" var="productList">
SELECT * FROM product p LEFT JOIN product_image pi ON pi.product_id = p.id
</sql:query>
<c:forEach var="item" items="${productList.rows}">
<img src="${item.img_url}">
<li>
<a href="${item.product_url}"><c:out value="${item.product_name}"/></a>
</li>
</c:forEach>
</ul>



<!-- 3.列表,变量赋值(产品名称+产品链接) -->
<ul class="">
<sql:query dataSource="${sqlDataSource}" var="productList">
SELECT * FROM product
</sql:query>
<c:forEach var="item" items="${productList.rows}">
<%-- 动态赋值 --%>
<c:set var="url" value="${item.product_url}"></c:set>
<li>
<a href="${url}"><c:out value="${item.product_name}"/></a>
</li>
</c:forEach>
</ul>



<!-- 4.列表,变量赋值(产品名称+产品链接) sql语句固定赋值 -->
<ul>
<c:set var="product_id" value="1"></c:set>
<sql:query dataSource="${sqlDataSource}" var="productList">
SELECT * FROM product p WHERE p.id = ${product_id}
</sql:query>
<c:forEach var="item" items="${productList.rows}">
<li>
<a href="${item.product_url}"><c:out value="${item.product_name}"/><a href="${url}">
</li>
</c:forEach>
</ul>



<!-- 5.列表,变量赋值(产品名称+产品链接+产品图片) sql语句动态赋值 -->
<sql:query dataSource="${sqlDataSource}" var="productList">
SELECT * FROM product p
</sql:query>
<c:forEach var="item" items="${productList.rows}">
<%-- 产品详情 --%>
<div class="imgInfo">
<h2><c:out value="${item.product_name}"/></h2>
<c:out value="${item.product_content}"/>
</div>
<%-- 动态赋值 --%>
<c:set var="product_id" value="${item.id}"></c:set>
<%-- 产品图片 --%>
<sql:query dataSource="${sqlDataSource}" var="productImgList" startRow="0" maxRows="1">
SELECT * FROM product_image pi WHERE pi.product_id = ${product_id}
</sql:query>
<c:forEach var="item2" items="${productImgList.rows}">
<img src="${item2.img_url}">
</c:forEach>
</c:forEach>

</body>
</html>