[quote] public int getTotalPage(int pageSize) { //pageSize为每页显示的条数
int count=0; //总的记录数
try {
conn = BaseDao.getConnection();
String sql="select count(*) as count from product";
pstmt=conn.prepareStatement(sql);
res = pstmt.executeQuery();
if(res.next()){
count = res.getInt("count"); //获得总的记录数
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
BaseDao.closeAll(conn, pstmt,res);
}
int totlePage = 1; //页数默认为1
int tempage = count % pageSize; //取余数,根据余数来判断页数
if(tempage==0) //当余数为0时,页数为一整数
{
totlePage = count / pageSize; //取商数为页数
}else {
totlePage = (int) (Math.floor(count / pageSize) + 1);//当页数有小数点时,去掉小数点后的数字,并为其加1
}
if(totlePage==0) //当算得页数为0时,给页数默认为1
{
totlePage = 1;
}
return totlePage;
}[/quote]
[quote][color=red]//返回某一页的记录数[/color]
public List getUserPage(int start,int pageSize) {//pageSize为每页显示的条数,start为起始数
List list = new ArrayList();
try {
conn = BaseDao.getConnection();
String sql = "SELECT TOP " + pageSize +
" * FROM product WHERE (id NOT IN (SELECT TOP " +
start +
" id FROM product ORDER BY id DESC ))ORDER BY id DESC";
pstmt=conn.prepareStatement(sql);
res = pstmt.executeQuery();
while(res.next())
{
Product produt=new Product();
produt.setId(res.getInt("id"));
produt.setName(res.getString("name"));
produt.setPictrueUrl(res.getString("pictureUrl"));
produt.setPictureName(res.getString("pictureName"));
produt.setPictureSmailName(res.getString("pictureSmailName"));
produt.setPictureSmailUrl(res.getString("pictureSmailUrl"));
produt.setPrice(res.getString("price"));
produt.setDescript(res.getString("descript"));
list.add(produt);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
BaseDao.closeAll(conn, pstmt,res);
}
return list;
}[/quote]
[quote]/**
* 获取总页数
*/
public int getTotalPage(int pageSize);
/**
* 获得每页的记录数
*
*/
public List getUserPage(int start,int pageSize);[/quote]
[quote]index.jsp页面代码
<%
int pages = 1; //默认的页数
String strPage = request.getParameter("page"); //获取文本框中翻页的数值
try{
if(strPage==null||strPage.equals("")){
pages=1;
}else{
pages = Integer.parseInt(strPage);
}
}catch(NumberFormatException e){
pages=1;
}
int pagesize = 2; //每页要显示的记录条数
ProductDao productDao=new ProductDaoImpl(); //实例化接口
//获得总的页数
int totalCount = productDao.getTotalPage(pagesize);
if(pages>totalCount){ //如果文本框中输入的值大于得到的总页数,那么页数默认为1
pages=1;
}
int start = pagesize * (pages-1); //起始页数
List list = productDao.getUserPage(start,pagesize); //得到每页要显示的信息
%>
<table width="100%" border="0">
<tr>
<td align="right"> <%
if(pages!=1){
%>
<a href="index.jsp?page=1">第一页</a>
<a href="index.jsp?page=<%=pages-1%>">上一页</a>
<%
}if(pages !=totalCount ){
%>
<a href="index.jsp?page=<%=pages+1%>">下一页</a>
<a href="index.jsp?page=<%=totalCount%>">最后一页</a>
<%
}
%>
<input type="text" size="3" name="page" value="<%=pages%>">
<input type="Submit" name="翻页" value="翻页">
页数:<%=pages %>/<%=totalCount %>
</td>
</tr>
</table>[/quote]
int count=0; //总的记录数
try {
conn = BaseDao.getConnection();
String sql="select count(*) as count from product";
pstmt=conn.prepareStatement(sql);
res = pstmt.executeQuery();
if(res.next()){
count = res.getInt("count"); //获得总的记录数
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
BaseDao.closeAll(conn, pstmt,res);
}
int totlePage = 1; //页数默认为1
int tempage = count % pageSize; //取余数,根据余数来判断页数
if(tempage==0) //当余数为0时,页数为一整数
{
totlePage = count / pageSize; //取商数为页数
}else {
totlePage = (int) (Math.floor(count / pageSize) + 1);//当页数有小数点时,去掉小数点后的数字,并为其加1
}
if(totlePage==0) //当算得页数为0时,给页数默认为1
{
totlePage = 1;
}
return totlePage;
}[/quote]
[quote][color=red]//返回某一页的记录数[/color]
public List getUserPage(int start,int pageSize) {//pageSize为每页显示的条数,start为起始数
List list = new ArrayList();
try {
conn = BaseDao.getConnection();
String sql = "SELECT TOP " + pageSize +
" * FROM product WHERE (id NOT IN (SELECT TOP " +
start +
" id FROM product ORDER BY id DESC ))ORDER BY id DESC";
pstmt=conn.prepareStatement(sql);
res = pstmt.executeQuery();
while(res.next())
{
Product produt=new Product();
produt.setId(res.getInt("id"));
produt.setName(res.getString("name"));
produt.setPictrueUrl(res.getString("pictureUrl"));
produt.setPictureName(res.getString("pictureName"));
produt.setPictureSmailName(res.getString("pictureSmailName"));
produt.setPictureSmailUrl(res.getString("pictureSmailUrl"));
produt.setPrice(res.getString("price"));
produt.setDescript(res.getString("descript"));
list.add(produt);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
BaseDao.closeAll(conn, pstmt,res);
}
return list;
}[/quote]
[quote]/**
* 获取总页数
*/
public int getTotalPage(int pageSize);
/**
* 获得每页的记录数
*
*/
public List getUserPage(int start,int pageSize);[/quote]
[quote]index.jsp页面代码
<%
int pages = 1; //默认的页数
String strPage = request.getParameter("page"); //获取文本框中翻页的数值
try{
if(strPage==null||strPage.equals("")){
pages=1;
}else{
pages = Integer.parseInt(strPage);
}
}catch(NumberFormatException e){
pages=1;
}
int pagesize = 2; //每页要显示的记录条数
ProductDao productDao=new ProductDaoImpl(); //实例化接口
//获得总的页数
int totalCount = productDao.getTotalPage(pagesize);
if(pages>totalCount){ //如果文本框中输入的值大于得到的总页数,那么页数默认为1
pages=1;
}
int start = pagesize * (pages-1); //起始页数
List list = productDao.getUserPage(start,pagesize); //得到每页要显示的信息
%>
<table width="100%" border="0">
<tr>
<td align="right"> <%
if(pages!=1){
%>
<a href="index.jsp?page=1">第一页</a>
<a href="index.jsp?page=<%=pages-1%>">上一页</a>
<%
}if(pages !=totalCount ){
%>
<a href="index.jsp?page=<%=pages+1%>">下一页</a>
<a href="index.jsp?page=<%=totalCount%>">最后一页</a>
<%
}
%>
<input type="text" size="3" name="page" value="<%=pages%>">
<input type="Submit" name="翻页" value="翻页">
页数:<%=pages %>/<%=totalCount %>
</td>
</tr>
</table>[/quote]