JSTL sql 标签
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'sqltlb.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<c:set value="${param.name}" var="name"></c:set>
<c:set value="${param.title}" var="title"></c:set>
<c:set value="${param.price}" var="price"></c:set>
<c:set value="${param.yr}" var="yr"></c:set>
<c:set value="${param.discription}" var="discription"></c:set><%--
<br>
${param.name }: ${not empty name }
<br>
${param.title }: ${not empty title }
<br>
${param.price }: ${not empty price }
<br>
${param.yr }: ${not empty yr }
<br>
${param.discription }: ${not empty discription }
<br>
<br>
${not empty name and not empty title and not empty price and not empty yr and not discription }
<br>
${not empty name and not empty title and not empty price and not empty
yr and not discription }
--%><br>
sql标签:
<br>
设定标签库:<%@ taglib uri="http://java.sun.com/jsp/jstl/sql"
prefix="sql"%>
<br>
<br>
设定数据源:<sql:setDataSource>
<br>
例:<sql:setDataSource >
<br>
<sql:setDataSource driver="com.mysql.jdbc.Driver" password="1234"
user="root" url="jdbc:mysql://localhost/bookdb" />
查询数据库:
<br>
<sql:query sql="select * from books"
var="rs"></sql:query>
<br>
<br>
<sql:query sql="select * from books" var="rs" scope="page"></sql:query>
<table border="1" bordercolor="000000" cellspacing="0" cellpadding="0"
width="830" height="149">
<tr>
<td bgcolor="#8080c0">
书号
</td>
<td bgcolor="#8080c0">
作者
</td>
<td bgcolor="#8080c0">
书名
</td>
<td bgcolor="#8080c0">
价格
</td>
<td bgcolor="#8080c0">
出版日期
</td>
<td bgcolor="#8080c0">
简介
</td>
</tr>
<c:forEach items="${pageScope.rs.rows}" var="row">
<tr>
<td>
${row.id}
</td>
<td>
${row.name}
</td>
<td>
${row.title}
</td>
<td>
${row.price}
</td>
<td>
${row.yr}
</td>
<td>
${row.description}
</td>
</tr>
</c:forEach>
</table>
<br>
<c:if test="${pageScope.rs.rowCount>0}">
数据查到 总数为:${rs.rowCount}
</c:if>
<br>
数据更新:
<br>
<sql:query sql="select max(id) id from books" var="rst"></sql:query>
<c:forEach items="${rst.rows}" var="idrows">
<c:set value="${idrows.id}" scope="page" var="idn"></c:set>
</c:forEach>
<br>
<form method="POST" name="upt" action="sqltlb.jsp">
<p>
ID号: ${pageScope.idn + 1}
<br>
作者:
<input type="text" name="name">
<br>
书名:
<input type="text" name="title">
<br>
价格:
<input type="text" name="price">
<br>
出版日期:
<input type="text" name="yr">
<br>
简介:
<input type="text" name="discription">
<br>
<input type="submit" name="send" value="提交">
<br>
<br>
<c:choose>
<c:when
test="${not empty name and not empty title and not empty price and not empty yr and not discription }">
<br>
可提交
<c:catch var="err">
<sql:update var="uptn"
sql="INSERT INTO books (id,name,title,price,yr,description,saleAmount) VALUES(?,?,?,?,?,?,0)">
<sql:param>
${pageScope.idn + 1}
</sql:param>
<sql:param>
${param.name }
</sql:param>
<sql:param>
${param.title }
</sql:param>
<sql:param>
${param.price}
</sql:param>
<sql:param>
${param.yr}
</sql:param>
<sql:param>
${param.discription }
</sql:param>
</sql:update>
</c:catch>
<c:if test="${! (empty err)}">
<c:out value="出错:${err}" />
</c:if>
<c:if test="${uptn>0}">
提交成功!
<c:redirect url="sqltlb.jsp"></c:redirect>
</c:if>
</c:when>
<c:otherwise>输入数据提交</c:otherwise>
</c:choose>
</p>
</form>
</body>
</html>