表结构:
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页面:
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);
%>
Article Paging DemoConnection 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();
%>