其实现和获取所有的商品是非常相似的。这里还用到了一个PReparedStatment的一个可以传参的属性。
public Book getBookByISBN(String isbn){ Connection conn = null; PreparedStatement preStmt = null; ResultSet bookSet = null; try{//--------------- 1. 连接数据库------------------ conn = DBHelper.getConnection();//--------------- 2. 执行SQL语句,接收执行结果集ResultSet ------------------ String sql = "select * from book where isbn=?;"; preStmt = conn.prepareStatement(sql); preStmt.setString(1, isbn); bookSet = preStmt.executeQuery();//--------------- 3. 处理执行结果集ResultSet ------------------ if(bookSet.next()){ Book book = new Book(); book.setAuthor(bookSet.getString("author")); book.setImg(bookSet.getString("img")); book.setIntro(bookSet.getString("intro")); book.setIsbn(bookSet.getString("isbn")); book.setName(bookSet.getString("name")); book.setPrice(Float.parseFloat(bookSet.getString("price"))); book.setPrice_original(Float.parseFloat(bookSet.getString("price_original"))); book.setPublish_company(bookSet.getString("publish_company"));// 时间格式化 SimpleDateFormat dateformat = new SimpleDateFormat( "yyyy年MM月dd日 "); ParsePosition pos = new ParsePosition(0); book.setPublish_time(dateformat.parse(bookSet.getString("name"),pos)); return book; }else{ return null; } }catch(Exception ex){ ex.printStackTrace(); return null; }finally{//--------------- 4. 必要的关闭ResultSet、Statement ------------------// 为了确保资源不被占用,需要在fianlly释放资源,注意这里不可以关闭Connection,否则会报错// 释放 ResultSet if(bookSet != null){ try{ bookSet.close(); bookSet = null; }catch(Exception ex){ ex.printStackTrace(); } }// 释放语句对象:PreparedStatement if(preStmt != null){ try{ preStmt.close(); preStmt = null; }catch(Exception ex){ ex.printStackTrace(); } } }}我们不难发现,index.jsp页和single页的头部和尾部是一样的,因此我们可以用JSP的3个编译指令(page、include和taglib)中的include指令来优化代码。 使用include指令,可以将一个外部文件嵌入到当前JSP文件中,同时解析这个页面中的JSP语句。语法如下:
<%@ include file="文件相对路径" %>所以,下面我们创建两个文件:header.jsp和footer.jsp分别存放网页的头部和尾部代码。然后single.jsp代码如下。同时用同样的方法修改index.jsp文件。 header.jsp
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE html><html lang="en"><head> <base href="<%=basePath%>"> <meta charset="UTF-8"> <title>DIAGRAM</title> <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" /> <link href="./bootstrap-3.3.0/CSS/bootstrap.min.css" rel="stylesheet"/> <link href="./css/style.css" rel="stylesheet"/> <link href="./css/single.css" rel="stylesheet"/></head><body> <nav class="navbar navbar-default" role="navigation"> <div class="logo-big"><span>D I A G R A M</span></div> <div class="container-fluid"> <div class="row nav-menuare"> <div class="navbar-header"> <a class="navbar-brand logo-small" href="#">DIAGRAM</a> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <div class="row navbar-row"> <div class="nav-divider col-md-3 col-sm-3"><span class="divider"></span></div> <div id="navbar" class="col-md-6 col-sm-6 collapse"> <ul> <li class="li-col"><a href="#">SHOPINDEX</a></li> <li class="li-col"><a href="#">NEWBOOK</a></li> <li class="li-col li-logo"><a href="./index.jsp"><img class="logo-img" src="./img/robot.png"/></a></li> <li class="li-col"><a href="#">CART(2)</a></li> <li class="li-col"><a href="#">CUSTOMER</a></li> </ul> </div> <div class="nav-divider col-md-3 col-sm-3"><span class="divider"></span></div> </div> </div> </div> </nav>footer.jsp
<div class="footer"> <div class="container-fluid"> <div class="row"> <div class="col-md-3"> </div> <div class="blog-coyp col-md-6"> <div class="blog text-left"><a href="https://github.com/donespeak" target="_blank">GITHUB</a> | <a href="http://blog.csdn.net/donespeak" target="_blank">CSDN</a> | EMAIL:yangguanr@Gmail.com </div><div class="copy text-right">©DoneSpeak.inc</div> </div> </div> </div> </div> <script src="./js/jquery-2.1.4.min.js"></script> <script src="./bootstrap-3.3.0/js/bootstrap.min.js"></script> <script src="./js/script.js"></script></body></html>single.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ page import="entity.Book" %><%@ page import="dao.BookDAO" %><%@ include file="header.jsp" %> <div class="main newbook-all"> <div class="container-fluid"> <div class="row"> <% BookDAO bookdao = new BookDAO(); Book book = bookdao.getBookByISBN(request.getParameter("isbn")); if(book != null){ %> <div class="col-md-3 col-md-offset-1 col-sm-4 col-sm-offset-1"> <div class="book-img"> <img src="img/<%= book.getImg() %>" /> </div> </div> <div class="sell col-md-5 col-sm-6"> <div class="booktext"> <h3>《<%= book.getName() %>》</h3> <div class="divider divider-bookinfo"></div> <p><span class="t1">作者:<%= book.getAuthor() %></span><span class="t1">著出版社:<%= book.getPublish_company() %></span><span class="t1">出版时间:<%= book.getPublish_time() %></span><p> <p class="newbook-intro">《<%= book.getIntro() %></p> </div> <div class="snapped-up-btn-area"> <span class="cost">¥<%= book.getPrice() %></span> / <span class="cost-original">¥<%= book.getPrice_original() %></span><button class="snapup">Add To Cart</button> </div> </div> <% } %> <div class="col-md-2 record"> <div class="rec-head">浏览记录</div> <div class="rec-card"> <a href="single.html"><img class="img-rec pull-left" src="img/图解网站分析.jpg"/></a> <div class="pull-left txt-rec">图解网站分析<br />¥34.8</div> </div> <div class="rec-card"> <a href="single.html"><img class="img-rec pull-left" src="img/图解网站分析.jpg"/></a> <div class="pull-left txt-rec">图解网站分析<br />¥34.8</div> </div> <div class="rec-card"> <a href="single.html"><img class="img-rec pull-left" src="img/图解网站分析.jpg"/></a> <div class="pull-left txt-rec">图解网站分析<br />¥34.8</div> </div> <div class="rec-card"> <a href="single.html"><img class="img-rec pull-left" src="img/图解网站分析.jpg"/></a> <div class="pull-left txt-rec">图解网站分析<br />¥34.8</div> </div> </div> </div> </div> </div><%@ include file="footer.jsp" %>include指令 以上使用include的方式属于静态的include指令,该指令会之间将指定文件中的内容嵌入到指令所在位置中,在进行编译。如果被嵌入的文件需要经常改变,建议使用操作指令。 是动作指令,其与上面的<%@include %>编译指令有很大不同,它不是将文件的内容直接嵌入,而是独立运行完之后,将生成的输出结果(html)嵌入,所以里面的java代码是不会被导入的。事实上,用指定的文件会独立的生成一个Servlet,而<%@ include%>不会。基本语法格式如下:
<jsp:include page="{relativeURL | <%=expression%>}" flush="true"/><%-- 或者 --%><jsp:include page="{relativeURL | <%=expression%>}" flush="true"> <jsp:param name="parameterNmae" value="parameterValue" /></jsp:include>(动态导入)与<%@ include%>(静态导入)的区别:
静态导入是代码完全融入被导入文件的代码,动态导入只是导入被导入的文件运行之后的内容; 静态导入是原文件和被导入文件融合成一个Servlet,而动态导入被导入文件会成为一个独立的于原文件的Servlet。 静态导入的编译指令有效,动态导入的编译指令失效;动态代码可以添加额外的参数。
此外,每个JSP开始的那行代码为page编译指令,还有<%@ page import="dao.BookDAO"%>
也是page编译指令。taglib编译指令的作用是用于定义和访问自定义标签。
新闻热点
疑难解答