By
yusijia
Updated:
刚学jsp和servlet时做的,jsp里加了java代码,这只是展示一下思路用
BookBean.java
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
| package com.mybooks;
public class BookBean { private int id; private String name; private double price; private int bookCount; private String author;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public int getBookCount() { return bookCount; }
public void setBookCount(int bookCount) { this.bookCount = bookCount; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
}
|
bookListForward.java
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
| package com.mybooks;
import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
import com.message.MyTools; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement;
public class BookListForward { private int recordCount; private int lastRow; private int pageCount; private int pageSize = 4; public int getPageSize(){ return pageSize; } public void setPageSize(int pageSize){ this.pageSize = pageSize; } public Connection getConnection(){ return MyTools.getConn(); } public int findDataRowCount(){ Connection conn = getConnection(); String sql = "select count(*) as totalCount from bookdatabase"; try{ Statement sta = (Statement) conn.createStatement(); ResultSet rs = sta.executeQuery(sql); if(rs.next()){ recordCount = rs.getInt("totalCount"); } rs.close(); conn.close(); }catch (SQLException e) { e.printStackTrace(); } return recordCount; } public int findLastRow(){ lastRow = findDataRowCount(); return lastRow; } public int findPageCount(){ recordCount = findDataRowCount(); pageCount = (recordCount % pageSize == 0)?(recordCount/pageSize):(recordCount/pageSize+1); return pageCount; } public List<BookBean> getPage(int pageNumber){ Connection conn = getConnection(); List<BookBean> list = new ArrayList<BookBean>(); int postion = (pageNumber - 1) * pageSize + 1; String sql = "select * from bookdatabase"; try{ Statement sta = (Statement) conn.createStatement(); ResultSet rs = sta.executeQuery(sql); rs.absolute(postion - 1); for(int i = 0; i < pageSize && rs.next(); i++){ BookBean book = new BookBean(); book.setId(rs.getInt("id")); book.setName(rs.getString("name")); book.setPrice(rs.getDouble("price")); book.setBookCount(rs.getInt("bookCount")); book.setAuthor(rs.getString("author")); list.add(book); } rs.close(); sta.close(); conn.close(); }catch (SQLException e) { e.printStackTrace(); } return list; } }
|
findbook2.jsp
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="com.mybooks.BookBean" %> <jsp:useBean id="blf" class="com.mybooks.BookListForward"></jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>分页显示图书列表</title> <style type="text/css"> td,th { padding: 5px; } .footerFloat{ float: right; } </style> <script type="text/javascript"> //判断用户是否选择了要删除的记录,如果是,则提示“是否删除”;否则提示“请选择要删除的记录” function checkdel(delid, formname) { var flag = false; for (i = 0; i < delid.length; i++) { if (delid[i].checked) { flag = true; break; } } if (!flag) { alert("请选择要删除的记录!"); return false; } else { if (confirm("确定要删除吗?")) { formname.submit(); } } } </script> </head> <body> <div width="98%" align="center"> <h2>分页显示图书列表</h2> </div> <form action="DelServlet" method="post" name="frm"> <div id="div1"> <table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#666666"> <tr> <th bgcolor="#FFFFFF">ID</th> <th bgcolor="#FFFFFF">图书名称</th> <th bgcolor="#FFFFFF">价格</th> <th bgcolor="#FFFFFF">数量</th> <th bgcolor="#FFFFFF">作者</th> <th bgcolor="#FFFFFF">删除</th> </tr> <%! int curPage = 1; %> <% String tmp = request.getParameter("curPage"); if(tmp==null){ tmp="1"; } curPage = Integer.parseInt(tmp); // 获取图书信息集合 List<BookBean> list = blf.getPage(curPage); // 判断集合是否有效 if(list == null || list.size() < 1){ out.println("<tr><td bgcolor='#FFFFFF' colspan='5'>没有任何图书信息!</td></tr>"); }else{ for(int i = 0; i < list.size(); i++){ %> <tr align="center"> <td bgcolor="#FFFFFF" ><%=list.get(i).getId()%></td> <td bgcolor="#FFFFFF"><%=list.get(i).getName()%></td> <td bgcolor="#FFFFFF"><%=list.get(i).getPrice()%></td> <td bgcolor="#FFFFFF"><%=list.get(i).getBookCount()%></td> <td bgcolor="#FFFFFF"><%=list.get(i).getAuthor()%></td> <td bgcolor="#FFFFFF"><input name="delid" type="checkbox" class="noborder" value="<%=list.get(i).getId()%>"></td> </tr> <% } } %> </table> </div> <br> <footer class="footerFloat"> <input type="button" value="全选" onclick="CheckAll()"/> <input type="button" value="不选" onclick="UnCheck()"/> <input type="button" value="反选" onclick="othercheck()"/> [<a style="color:red;cursor:pointer;" onClick="checkdel(frm.delid,frm)">删除</a>] <div id="ch" style="display: none"> <input name="delid" type="checkbox" value="0"> </div> <!--层ch用于放置隐藏的checkbox控件,因为当表单中只是一个checkbox控件时,应用javascript获得其length属性值为undefine--> </footer> </form> <!-- 注意:下面的script代码放在head标签里则无效 --> <script> var CheckBox=document.getElementById('div1').getElementsByTagName('input'); //全选 function CheckAll(){ for(i=0;i<CheckBox.length;i++){CheckBox[i].checked=true;}; }; //不选 function UnCheck(){ for(i=0;i<CheckBox.length;i++){CheckBox[i].checked=false;}; }; //反选 function othercheck(){ for(i=0;i<CheckBox.length;i++){ if(CheckBox[i].checked==true){ CheckBox[i].checked=false; }else{ CheckBox[i].checked=true; } } }; </script> <br> 第<%=curPage %>页(共<%=blf.findPageCount() %>页) <br> <a href="findbook2.jsp?curPage=1">首页</a> <a href="findbook2.jsp?curPage=<%=curPage-1%>">上一页</a> <% int pageCount = 3; //根据pageCount的值显示每一页的数字并附加上相应的超链接 for(int i=1; i <= pageCount; i++){ %> <a href="findbook2.jsp?curPage=<%=i%>"><%=i%></a> <% } %> <a href="findbook2.jsp?curPage=<%=curPage+1%>">下一页</a> <a href="findbook2.jsp?curPage=<%=blf.findPageCount()%>">末页</a> <!-- 通过表单提交用户想要显示的页数 --> <form action="findbook2.jsp" method="get"> 跳转到第<input type="text" name="curPage" size="4">页 <input type="submit" name="submit" value="跳转"> </form> </body> </html>
|