日期:2014-05-18  浏览次数:20731 次

求教:JSP+SQLSERVER的分页问题
JSP+SQLSERVER的分页问题

package bean; 
import java.sql.*; 
public class conn { 
Connection con=null; 
ResultSet rs=null; 
public conn(){ 
try{ 
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 

catch(ClassNotFoundException e){ 
System.err.println(e.getMessage()); 


public ResultSet executeQuery(String sql){ 
try{ 
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=andy","sa",""); 
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); 
rs=stmt.executeQuery(sql); 

catch(SQLException e){ 
System.err.println(e.getMessage()); 

return rs; 

public int executeUpdate(String sql){ 
int result=0; 
try{ 
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=andy","sa",""); 
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
result=stmt.executeUpdate(sql); 

catch(SQLException ex){ 
System.out.println(ex.getMessage()); 

return result; 



jsp文件:<%@ page contentType="text/html; charset=GBK" %> 
<%@ page language="java" import="java.sql.*"%> 
<%@ page import="java.lang.*"%> 
<jsp:useBean id="connD" scope="page" class="bean.conn"/> 
<%! 
ResultSet rs=null; 
ResultSet rsTmp=null; 
String sql=""; 
int PageSize=6; 
int Page=3; 
int totalPage=1; 
String str=""; 

public String ShowOnePage(ResultSet rs,int Page,int PageSize){ 
str=""; 
try{ 
rs.absolute((Page-1)*PageSize+1); 

catch(SQLException e){ 

for(int iPage=1;iPage<=PageSize;iPage++){ 
str+=RsToGbook(rs); 
try{ 
if(!rs.next()) break; 

catch(Exception e){} 

return str; 


public String RsToGbook(ResultSet rs){ 
String tt=""; 
try{ 
tt+="<tr>"; 
tt+="<td>"+rs.getInt(1)+"</td>"; 
tt+="<td>"+rs.getString(2)+"</td>"; 
tt+="<td>"+rs.getString(3)+"</td>"; 
tt+="<td>"+rs.getInt(4)+"</td>"; 
tt+="</tr>"; 

catch(SQLException e){} 
return tt; 

%> 
<% 
sql="select * from Stu"; 
try{ 
rs=connD.executeQuery(sql); 

catch(Exception e){ 
out.println("访问数据库出错"); 

%> 
<html> 
<head> 
<title>分页浏览数据库技巧</title> 
</head> 
<body bgcolor="#FFFFFF"> 
<h2 align="center">JSP中的分页控制</h2> 
<hr> 
<center>