这是Dao层的代码
一个是分页查询
一个是查询一共多少条记录package Dao;
import java.util.ArrayList;
import java.util.List;
import Entry.BookInfo;
public class BookInfoDaoImpl extends BaseDao implements BookInfoDao {
public List<BookInfo> showAllBook(int page,int pageIndex) {
openConnection();
List<BookInfo> bookInfoList=new ArrayList<BookInfo>();
String sqlString="select top "+page+" * from bookinfo where bookid not in(select top (("+pageIndex+"-1)*"+page+") bookid from bookinfo)";
try {
stmt=conn.prepareStatement(sqlString);
rs=stmt.executeQuery();
while (rs.next()) {
BookInfo bookInfo=new BookInfo();
bookInfo.setId(rs.getString("bookid"));
bookInfo.setBookName(rs.getString("bookname"));
bookInfo.setBookType(rs.getString("booktype"));
bookInfo.setBookStatus(rs.getString("bookstatus"));
bookInfoList.add(bookInfo);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
closeAll();
}
return bookInfoList;
}
public int bookInfoCount() {
openConnection();
String sqlString="select count(*) from bookinfo";
int i=0;
try {
stmt=conn.prepareStatement(sqlString);
rs=stmt.executeQuery();
if(rs.next()){
i=rs.getInt(1);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
closeAll();
}
return i;
}
}
---------------------------------------------------------------------------------------
下面是JSP页面的内容
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@page import="Biz.BookInfoBiz"%>
<%@page import="Biz.BookInfoBizImpl"%>
<%@page import="Entry.BookInfo"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" 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 'index.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>
<table border="1">
<tr>
<td>图书名称</td>
<td>类别</td>
<td>状态</td>
</tr>
<%
BookInfoBiz bb=new BookInfoBizImpl();
int book_count=bb.bookInfoCount();
int page_size=5;
int page_count=book_count%page_size==0?book_count/page_size:book_count/page_size+1;
int pageIndex=1;
String pageIndex_str=(String)request.getAttribute("pageIndex");
if(pageIndex_str!=null){
pageIndex=Integer.valueOf(pageIndex_str);
}
List<BookInfo> bookinfoList=bb.showAllBook(page_size,pageIndex);
for(int i=0;i<bookinfoList.size();i++){
BookInfo bookinfo=bookinfoList.get(i);
%>
<tr>
<td><%=bookinfo.getBookName() %></td>
<td><%=bookinfo.getBookType() %></td>
<td><%=bookinfo.getBookStatus() %></td>
</tr>
<%
}
%>
<p><a href="index.jsp?pageIndex=<%=pageIndex-1 %>">上一页</a><a href="index.jsp?pageIndex=<%=pageIndex+1 %>">下一页</a></p>
</table>
</body>
</html>各位帮忙看看那里错了。上一页和下一页都不好使
一个是分页查询
一个是查询一共多少条记录package Dao;
import java.util.ArrayList;
import java.util.List;
import Entry.BookInfo;
public class BookInfoDaoImpl extends BaseDao implements BookInfoDao {
public List<BookInfo> showAllBook(int page,int pageIndex) {
openConnection();
List<BookInfo> bookInfoList=new ArrayList<BookInfo>();
String sqlString="select top "+page+" * from bookinfo where bookid not in(select top (("+pageIndex+"-1)*"+page+") bookid from bookinfo)";
try {
stmt=conn.prepareStatement(sqlString);
rs=stmt.executeQuery();
while (rs.next()) {
BookInfo bookInfo=new BookInfo();
bookInfo.setId(rs.getString("bookid"));
bookInfo.setBookName(rs.getString("bookname"));
bookInfo.setBookType(rs.getString("booktype"));
bookInfo.setBookStatus(rs.getString("bookstatus"));
bookInfoList.add(bookInfo);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
closeAll();
}
return bookInfoList;
}
public int bookInfoCount() {
openConnection();
String sqlString="select count(*) from bookinfo";
int i=0;
try {
stmt=conn.prepareStatement(sqlString);
rs=stmt.executeQuery();
if(rs.next()){
i=rs.getInt(1);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
closeAll();
}
return i;
}
}
---------------------------------------------------------------------------------------
下面是JSP页面的内容
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@page import="Biz.BookInfoBiz"%>
<%@page import="Biz.BookInfoBizImpl"%>
<%@page import="Entry.BookInfo"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" 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 'index.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>
<table border="1">
<tr>
<td>图书名称</td>
<td>类别</td>
<td>状态</td>
</tr>
<%
BookInfoBiz bb=new BookInfoBizImpl();
int book_count=bb.bookInfoCount();
int page_size=5;
int page_count=book_count%page_size==0?book_count/page_size:book_count/page_size+1;
int pageIndex=1;
String pageIndex_str=(String)request.getAttribute("pageIndex");
if(pageIndex_str!=null){
pageIndex=Integer.valueOf(pageIndex_str);
}
List<BookInfo> bookinfoList=bb.showAllBook(page_size,pageIndex);
for(int i=0;i<bookinfoList.size();i++){
BookInfo bookinfo=bookinfoList.get(i);
%>
<tr>
<td><%=bookinfo.getBookName() %></td>
<td><%=bookinfo.getBookType() %></td>
<td><%=bookinfo.getBookStatus() %></td>
</tr>
<%
}
%>
<p><a href="index.jsp?pageIndex=<%=pageIndex-1 %>">上一页</a><a href="index.jsp?pageIndex=<%=pageIndex+1 %>">下一页</a></p>
</table>
</body>
</html>各位帮忙看看那里错了。上一页和下一页都不好使
解决方案 »
- jsp 首頁的登錄信息問題
- AXIS1.4 调用webservice时出现java.util.zip.ZipException: error in opening zip file
- JSP 刷新重复提交
- 用struts的validate验证,输出的错误信息是乱码是怎么回事啊.进者有分
- 没有合适的驱动??
- 购物车问题
- 为什么这样提交页面,接下来的程序总是执行两次?
- Excel中怎么设置单元格的背景颜色?
- jsp怎么获得网页地址?
- jsp表单数据不能通过jdbc把数据输入到sql server中去,在tomcat控制台上显示:"not suitable driver"
- wsdl文件用axis编译出来的java类,各个类有什么作用呢?
- 关于webservice的问题
什么显示都没?
包错了?
你的SQL语句贴全了?
这句话不报错?有这个类吗?
我也是这么写的,附上代码:跟你的思路是一样的if (request.getParameter("review_state") != null || request.getParameter("page") != null) {
int pagecount = 20; //一页显示的记录数
int rowcount = 0; //记录总数
int sumpagecount = 0; //总页数
int currentPage = 1; //待显示页码 //取得待显示页码
String strPage = request.getParameter("page");
if (strPage == null) {
//表明在QueryString中没有page这一个参数,此时显示第一页数据
currentPage = 1;
} else {
//将字符串转换成整型
currentPage = Integer.parseInt(strPage);
if (currentPage < 1)
currentPage = 1;
} //获取记录总数
paging sum = new paging();
rowcount = sum.sumpage(sqlcond);
//记算总页数
sumpagecount = sum.sumpagecount(pagecount, sqlcond);
//调整待显示的页码
if (sumpagecount == 0)
sumpagecount++;
if (currentPage > sumpagecount)
currentPage = sumpagecount; List<Member> memberlist = sum.fenye(currentPage, pagecount,
sqlcond);
System.out.println("currentPage" + currentPage);
System.out.println("pagecount" + pagecount); request.setAttribute("member", memberlist);
给个demo,要么?