<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>平板显示</title> </head> <% int pageSize = 3; String strPageNo = request.getParameter("pageNo"); int pageNo = 1; if(strPageNo == null || strPageNo.equals("")){ pageNo = 1; }else{ //处理是否字符串 try{ pageNo = Integer.parseInt(strPageNo.trim()); }catch(NumberFormatException e){ pageNo = 1; } //判断是否为负数 if(pageNo <= 0) pageNo = 1; } Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=****"; Connection conn = DriverManager.getConnection(url); //获得总记录数 Statement stmtCount = conn.createStatement(); ResultSet rsCount = stmtCount.executeQuery("select count(*) from article where pid = 0"); rsCount.next(); int totalRecords = rsCount.getInt(1); //算得总页数 int totalPages = totalRecords % pageSize == 0 ? totalRecords / pageSize : totalRecords /pageSize + 1; //判断页数是否过头 if(pageNo > totalPages) pageNo = totalPages; //这里计算当前页 int startPos = (pageNo - 1) * pageSize; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from article where pid = 0 order by pdate desc limit " + startPos + ", " + pageSize); %> <body> <a href="Post.jsp">发表新贴</a> <table border="1"> <% while(rs.next()){ %> <tr> <td><a href=""><%=rs.getString("cont")%></a></td> </tr> <% } %> </table> 总有<%=totalPages %>页;现在是第<%=pageNo %>页 <br /> <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo-1 %>"> << </a> <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo+1 %>"> >> </a> <form action="" name="form1"> <select name="pageNo" onchange="document.form1.submit()"> <% for(int i = 1; i <= totalPages; i++){ %> <option value=<%=i %> <%=(pageNo == i)? "selected" : "" %>>第<%=i %></option> <% } %> </select> </form> <!-- --> <form name="form2" action="ShowArticleFlat.jsp"> <input type="text" size="4" name="pageNo" value="<%=pageNo %>" /> <input type="submit" value="Go>>" /> </form> <% rs.close(); stmt.close(); conn.close(); %> </body> </html>
create database bbs; use bbs; create table article ( id int primary key auto_increment, pid int, rootid int, title varchar(255), cont text, pdate datetime, isleaf int ); insert into article values (null, 0, 1, '蚂蚁大战大象', '蚂蚁大战大象', now(), 1); insert into article values (null, 1, 1, '大象被打趴下了', '大象被打趴下了',now(), 1); insert into article values (null, 2, 1, '蚂蚁也不好过','蚂蚁也不好过', now(), 0); insert into article values (null, 2, 1, '瞎说', '瞎说', now(), 1); insert into article values (null, 4, 1, '没有瞎说', '没有瞎说', now(), 0); insert into article values (null, 1, 1, '怎么可能', '怎么可能', now(), 1); insert into article values (null, 6, 1, '怎么没有可能', '怎么没有可能', now(), 0); insert into article values (null, 6, 1, '可能性是很大的', '可能性是很大的', now(), 0); insert into article values (null, 2, 1, '大象进医院了', '大象进医院了', now(), 1); insert into article values (null, 9, 1, '护士是蚂蚁', '护士是蚂蚁', now(), 0);
出自尚学堂_马士兵老师的视频!!
应该这样发????呵呵!