JSP2.0 学习之路 5

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>
  设定标签库:&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/sql&quot;
  prefix=&quot;sql&quot;%&gt;
  <br>
  <br>
  设定数据源:&lt;sql:setDataSource&gt;
  <br>
  例:&lt;sql:setDataSource &gt;
  <br>
  <sql:setDataSource driver="com.mysql.jdbc.Driver" password="1234"
   user="root" url="jdbc:mysql://localhost/bookdb" />
  查询数据库:
  <br>
  &nbsp;&lt;sql:query sql=&quot;select * from books&quot;
  var=&quot;rs&quot;&gt;&lt;/sql:query&gt;
  <br>
  &nbsp;
  <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}&nbsp;
     </td>
     <td>
      ${row.name}&nbsp;
     </td>
     <td>
      ${row.title}&nbsp;
     </td>
     <td>
      ${row.price}&nbsp;

     </td>
     <td>
      ${row.yr}&nbsp;
     </td>
     <td>
      ${row.description}&nbsp;
     </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>

    &nbsp;
    <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>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>动漫搜索</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"> --> <script type="text/javascript"> var xmlhttp; function loadXMLDoc(url,cfun) { // alert("hadhfaf"); xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=cfun; xmlhttp.open("GET",url,true); xmlhttp.send(); } function myfunction(id,times) { //alert("123"); loadXMLDoc("open.jsp?id="+id+"&times="+times+"&t="+Math.random(),function() { //alert(xmlhttp.status); if(xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById(id).innerHTML=xmlhttp.responseText; } }); } </script> <% //currentPage当前显示页数 if (request.getParameter("currentPage") == null||request.getParameter("currentPage").equals("")) { pageContext.setAttribute("currentPage", new Integer(0)); } else { pageContext.setAttribute("currentPage", request .getParameter("currentPage")); } %> <sql:query var="rs" dataSource="jdbc/mysql"> select* from anime </sql:query> <!-- 得到所有记录数 --> <c:set var="resultNum"> <jsp:getProperty name="rs" property="rowCount" /> </c:set> <!-- 每页显示记录数 --> <c:set var="PerPageNum" value="3" /> </head> <style type="text/css"> body{background-color:} </style> <body> <% int perPageNum = Integer.parseInt((String) pageContext .getAttribute("PerPageNum")); int resultNum = Integer.parseInt((String) pageContext .getAttribute("resultNum")); int pageNum; //如果所有记录数除每页显示记录数没有余数-1 if((int)resultNum%perPageNum==0) pageNum = (int)(resultNum / perPageNum-1); else pageNum = (int)(resultNum / perPageNum); pageContext.setAttribute("pageNum", pageNum); %> <div style="border:1px solid #cccccc; width:1100px;margin:100px 0 0 90px"> <table style=" width:1100px;"> <tr> <td>序号</td> <td>热搜动漫</td> <td>动漫简介</td> <td>更新集数</td> <td>搜索指数</td> <td>动漫详情</td> </tr> <c:if test="${currentPage lt 0 }"><!-- 当前显示页小于0 --> <c:set var="currentPage" value="0"></c:set> </c:if> <c:if test="${currentPage gt pageNum}"><!-- 当前显示页大于总页数 --> <c:set var="currentPage" value="${pageNum }"></c:set> </c:if> <c:set var="currentPage" value="${currentPage}" scope="session" /> <!-- 循环查找记录 --> <c:forEach var="row" items="${rs.rows}" begin="${currentPage*PerPageNum}" end="${PerPageNum+currentPage*PerPageNum-1}" > <tr> <td> ${row.id }</td> <td> <img src="image/${row.image}" width="50" height="50"/></td> <td style="width:600"> ${row.introduce}</td> <td> ${row.blues} </td> <td> ${row.times }</td> <c:set var="times" value="${row.times }" scope="session" /> <td> <input type="button" value="展开" onclick="myfunction(${row.id },${row.times })"/> </td> </tr> <tr> <td colspan=6 id="${row.id}"></td> </tr> </c:forEach> </table> <div style="margin:0px 0px 0px 800px"> 总共有${pageNum+1}页-第${currentPage+1}页-<a href="index.jsp?currentPage=${currentPage-1}">上一页</a> - <a href="index.jsp?currentPage=${currentPage+1}">下一页</a> </div> </div> </body> </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值