<%
Statement sql;
ResultSet rs;
sql=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select id,title,date from news order by date desc");
int showpage=1,pageCount=1,pageSize=15;
//显示页码,总页码数,每页显示多少条
rs.last();
int lastRow=rs.getRow();
//最后一行的行号,就是一共有lastRow这么多条
pageCount=(lastRow%pageSize==0)?(lastRow/pageSize):(lastRow/pageSize+1); //求总页码数
String integer=request.getParameter("showPage");
if(integer==null){integer="1";}
try{showpage=Integer.parseInt(integer);}
catch(NumberFormatException e){showpage=1;}
if(showpage<1){showpage=1;}
if(showpage>pageCount){showpage=pageCount;}
int posion=(showpage-1)*pageSize+1;
rs.absolute(posion);
for(int i=0;i<pageSize;i++)
{
out.print("<li><a href='viewNews.jsp?id="+rs.getInt("id")+"'>"+rs.getString("title")+"</a>&nbsp;&nbsp;&nbsp;&nbsp;"+rs.getString("date").substring(0,10)+"</li>");
if(!rs.next()){break;}
}
%>
<%
out.print("&nbsp;&nbsp;<a href='newsList.jsp?showPage=1'>|&lt;</a>&nbsp;&nbsp;");
for(int j=1;j<=pageCount;j++)
{
if(showpage==j)
{
out.print("<a href='newsList.jsp?showPage="+j+"'><font class='underline'>"+j+"</font></a>&nbsp;&nbsp;");
}
else{out.print("<a href='newsList.jsp?showPage="+j+"'>"+j+"</a>&nbsp;&nbsp;");}
}
out.print("<a href='newsList.jsp?showPage="+pageCount+"'>&gt;|</a>");
%>