public abstract class BaseJdbcDao { private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static final String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=notetest"; private static final String DBUSER="sa"; private static final String DBPASS="sa";
public class noteDao extends BaseJdbcDao{ int count=0; //得到所有记录数 public int getNoteCount() { String sql1="select count(*) from note"; int pageCount=0; conn=super.getConn(); try{ pstmt=conn.prepareStatement(sql1); rst=pstmt.executeQuery(); rst.next(); count=rst.getInt(1);
}catch(SQLException e) { e.toString(); }finally { super.CloseAll(); } return count; } //分页显示 public List ShowNotesByPage(int page,int pageSize) { List listnote=new ArrayList(); note nn=null; int preCount = pageSize*(page-1); int pageCount=0; String sql="select top "+pageSize+" * from note where id not in (select top "+preCount+" id from note order by id desc) order by id desc"; conn=super.getConn(); try{ if(count%pageSize==0){ pageCount=count/pageSize; } else { pageCount=count/pageSize+1; } pstmt=conn.prepareStatement(sql); rst=pstmt.executeQuery(); while(rst.next()) { nn=new note(); nn.setId(rst.getInt("id")); nn.setTitle(rst.getString("title")); nn.setAuthor(rst.getString("author")); nn.setContent(rst.getString("content")); listnote.add(nn); } }catch(SQLException e) { e.toString(); }finally { super.CloseAll(); } return listnote; } }
//页面中的代码 <%@ page language="java" import="java.util.*,entity.*,dao.*" pageEncoding="gbk"%> <% 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%>">
List list=new ArrayList(); note nn=null; noteDao notedao=new noteDao(); int count=notedao.getNoteCount(); int pageSize =5; int currentPage = 1; int pagecount;