JavaWeb之JSTL和EL表达式实例(连接mysql数据库提取内容)

在使用前请准备好所使用的JSTL包

配置信息:
数据库名称:mydata
数据库密码:123456789
表名称:goods(货物)
表属性:name(名称(varchar),价格(decimal),数量(int))

<%@page contentType="text/html" 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>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>showData</title>
	</head>
<body>
	<sql:setDataSource var="database" driver="com.mysql.jdbc.Driver" 
						url="jdbc:mysql://localhost:3306/mydata?useUnicode=true&characterEncoding=utf-8" 
						user="root" password="123456789"></sql:setDataSource>
	<sql:query dataSource="${database}" var="results">
		select * from goods;
	</sql:query>
	<table border="1">
		<c:forEach items="${results.rows}" var="perRow">
			<tr>
				<td>${perRow.name}</td>
				<td>${perRow.price}</td>
				<td>${perRow.count}</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

发布了68 篇原创文章 · 获赞 12 · 访问量 5221

猜你喜欢

转载自blog.csdn.net/qq_40963076/article/details/104397929