日期:2014-05-16  浏览次数:20397 次

jsp+mysql文章内容分页显示
表结构:  
  
CREATE TABLE `article` (                                 
           `id` int(10) unsigned NOT NULL AUTO_INCREMENT,         
           `content` text,                                        
           PRIMARY KEY (`id`)                                     
         ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8   
  
   
  
JSP页面:  
  
   
  
<%@ page language="java" import="java.sql.*,java.io.*" pageEncoding="GBK"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
  
<%  
 int size = 4096;       //每一页显示的字符数   
 int currentPage = 0;   //当前页,初始化为0   
 boolean hasNext = false; //是否有下一页   
 String p = request.getParameter("p");  
 if(p != null && !"".equals(p))  
  currentPage = Integer.parseInt(p);  
   
 %>  
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <base href="<%=basePath%>">  
      
    <title>Article Paging Demo</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("com.mysql.jdbc.Driver");  
      conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");  
      ps = conn.prepareStatement("select content from article where id=1");  
      rs = ps.executeQuery();  
        
      while(rs.next()) {  
       InputStreamReader isr = new InputStreamReader(rs.getBinaryStream("content"), "UTF-8");  
    BufferedReader br = new BufferedReader(isr);  
    //跳过size*currentPage个字符,从第(size*currentPage + 1)个字符开始读   
    br.skip(size * currentPage);  
    StringBuilder _str = new StringBuilder();  
    char[] buffer = new char[1024];  
    int count = 0;  
    while(br.read(buffer) != -1) {  
     if(count*1024 > 4096) {  
      hasNext = true;   //如果br.read() != -1 则表明有下一页   
      break;  
     }  
     _str.append(buffer);  
     count ++;  
    }  
    out.println(_str);  
      }  
  
   
  
      rs.close();  
      ps.close();  
      conn.close();  
     %>  
       
     <br />  
     <div align="center">  
     <%  
      if((currentPage - 1) >= 0) {  
       out.println("<a href="/" mce_href="/""index.jsp?p=" + (currentPage - 1) + "/" >上一页</a>&nbsp;&nbsp;");  
      } else {  
       out.println("<a >上一页</a>&nbsp;&nbsp;");  
      }  
      if(hasNext) {  
       out.println("<a href="/" mce_href="/""index.jsp?p=" + (currentPage + 1) + "/" >下一页</a>");  
      } else {  
       out.println("<a >下一页</a>");  
      }  
      %>  
    </div>  
  </body>  
</html>  

?