251万条数据进行查询仅需7秒左右
--此存储过程,将返回3个结果集,第一个空集,第二个信息集,第三个数据集
create procedure proc_fenye
@sqlstr nvarchar(4000),--查询字符串
@currentpage int,--第N页
@pagesize int--每页行数
as
set nocount on
declare @P1 int,--P1是游标的id
@rowcount int,@totalpage int
exec sp_cursoropen @P1
output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount
output
set @totalpage=ceiling(1.0*@rowcount/@pagesize)
select @currentpage as currpage,@totalpage as totalpage,@rowcount
as totalrow
if @currentpage>@totalpage set
@currentpage=@totalpage
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user="sa"; String password="";
Connection con=
DriverManager.getConnection(url,user,password);
CallableStatement stat = con.prepareCall("{call
proc_fenye(?,?,?)}");
stat.setString(1,"select * from t_tmp");//给存储过程传递参数
stat.setInt(2,2);
stat.setInt(3,10);
stat.execute();//执行指定的存储过程,并返回多个结果集
stat.getMoreResults();//移动到第二个
ResultSet rs1=stat.getResultSet();//返回信息集
stat.getMoreResults();//移动到第三个
ResultSet rs2=stat.getResultSet();//返回数据集
System.out.println(rs1.getString(1)); System.out.println(rs2.getString(1));
//getMoreResults();移动到此 Statement
对象的下一个结果,如果其为 ResultSet 对象,则返回 true,并隐式关闭利用方法 getResultSet 获取的所有当前
ResultSet 对象。