jsp新闻内容分页

<%@ page language="java" import="java.sql.*,java.io.*" pageEncoding="gb2312"%>
<%
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>jsp新闻内容分页</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">
  </head>
 
  <body>
 
    <%
      Connection conn = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:mydb","test","test");
      ps = conn.prepareStatement("select content from test where id=1");
      rs = ps.executeQuery();
      String content =null;
      while(rs.next()){
     content = rs.getString("content");
      }
 
     int numStr =5;   //每页显示多少字符
     int countStr;   //一共有多少字符数
     int intPage=-1; //待显示页码
     int intPageCount; //总页数
 
     countStr =content.length();
     if(countStr%numStr==0)
     {
     intPageCount = countStr / numStr ;//共几页
     }else{
     intPageCount = countStr / numStr +1;
     } 
   
     %>
   <%
      rs.close();
      ps.close();
      conn.close();
     %>
      <%
if(request.getParameter("pages")==null)
intPage=1;
else
intPage=Integer.parseInt(request.getParameter("pages"));
int startNumStr=(intPage-1)*numStr;//当前页开始字符数位置
int endNumStr=(intPage)*numStr;//当前页结尾字符数位置

while(startNumStr<endNumStr)
{
if((intPage-1)< countStr / numStr){
out.print(content.substring((intPage-1)*numStr,(intPage)*numStr));
}else{
out.print(content.substring((intPage-1)*numStr,countStr));
}
startNumStr=startNumStr+numStr;
}
%>
    
     <br />
<table width="80%"  border="0" align="center" cellpadding="1" cellspacing="0">
  <tr>
    <td width="60%" >
    <%
    if(countStr>numStr){
     %>
    共<%=intPageCount %>,当前第<%=intPage%>页&nbsp;&nbsp;
    <a href="MyJsp1.jsp?pages=1" >首页</a>

<%
if((intPage-1)<1)
out.print("上一页");
else
out.print("<a href='MyJsp1.jsp?pages="+(intPage-1)+"''>上一页</a>");
%>

<%
if(intPage<intPageCount)
out.print("<a href='MyJsp1.jsp?pages="+(intPage+1)+"''>下一页</a>");
else
out.print("下一页");
%>
&nbsp;<a href='MyJsp1.jsp?pages=<%=intPageCount%>''> 尾页</a>
<%
}
%>
</td>
  </tr>
</table>

  </body>
</html>

你可能感兴趣的:(oracle,sql,jsp,jdbc,sun)