人都弄晕了

废话不说 先贴代码:

<%@ page language = "java" pageEncoding="GB18030"%>
<%@ page import = "java.sql.*" %>

<%
String strPageNum = request.getParameter("pageNum");
int pageNum = 0;//当前第几页
int pageNumber = 0;//当前页的起始位置
int pageSize = 3;//每页所显示的记录数
try{
	if(strPageNum == null || "".equals(strPageNum) || Integer.parseInt(strPageNum) <=1){
		pageNumber = 0;
		pageNum = 1;
	} else{
		pageNum = Integer.parseInt(strPageNum);
		pageNumber = pageNum * pageSize - 2;
		//pageNumber = (pageNum - 1) * pageSize;
	}
} catch(NumberFormatException e) {
	pageNumber = 0;
}

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=rinimama";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String sql2 = "select count(pid) from article where pid = 0";
ResultSet rs1 = stmt.executeQuery(sql2);
rs1.next();
int totleRecords = rs1.getInt(1);
int totlePages = totleRecords % pageSize == 0 ? totleRecords / pageSize : totleRecords / pageSize + 1 ;
if(pageNum > totlePages) {
	pageNum = totlePages;
}
String sql = "select * from article where pid = 0 order by pdate desc limit "+ pageNumber +", "+pageSize ;
ResultSet rs = stmt.executeQuery(sql);
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>My JSP 'ShowArticleFlat.jsp' starting page</title>
  </head>
  
  <body>
    <h3 align="center"><a href="new_theme.jsp">发表新帖</a></h3>
    <table border="1" align="center">
<%
	while(rs.next()) {
%>
		<tr>
			<td><%=rs.getString("title") %></td>
		</tr>
<%		
	}
rs.close();
stmt.close();
conn.close();
%>
    </table>
    <h3 align="center">
		共<%=totlePages %>页  当前第<%=pageNum %>页  <a href="ShowArticleFlat.jsp?pageNum=<%=pageNum - 1 %>"> 上一页 </a><a href="ShowArticleFlat.jsp?pageNum=<%=pageNum + 1 %>"> 下一页 </a><a href="ShowArticleFlat.jsp"> 首页 </a><a href="ShowArticleFlat.jsp?pageNum=<%=totlePages %>"> 尾页 </a>   
    </h3>
  </body>
</html>

 

 弄了一晚上,总是有一个数据插不进去,后来才发现,原来 sql 语句中的 limit 这个东西 是充0开始的 而不是从1开始的 闷死!!!!

你可能感兴趣的:(sql,jsp,mysql,jdbc,bbs)