談談Java調(diào)用SQL Server分頁存儲過程
作者:chenyunhong
本文主要談談Java調(diào)用SQL Server分頁存儲的過程,其返回是多個結果集,只要呈現(xiàn)形式是代碼,文字不多,簡單易懂。
本文主要談談Java調(diào)用SQL Server分頁存儲的過程,其返回是多個結果集,只要呈現(xiàn)形式是代碼,文字不多,簡單易懂。
SQL存儲過程:
- USE [Db_8za8za_2]
- GO
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- -- =============================================
- -- Description: <Description,,通用分頁存儲過程>
- -- =============================================
- ALTER PROCEDURE [dbo].[paging ]
- -- Add the parameters for the stored procedure here
- --傳入?yún)?shù)
- @SqlStr nvarchar(4000), --查詢字符串
- @CurrentPage int, --第N頁(當前頁數(shù))
- @PageSize int --每頁行數(shù)
- AS
- BEGIN
- -- SET NOCOUNT ON added to prevent extra result sets from
- -- interfering with SELECT statements.
- SET NOCOUNT ON;
- --定義變量
- DECLARE @CursorId int --CursorId是游標的id
- DECLARE @Rowcount int --總記錄(行)數(shù)
- DECLARE @pageCount int --總頁數(shù)
- -- Insert statements for procedure here
- EXEC sp_cursoropen @CursorId output,@SqlStr,
- @Scrollopt=1,@Ccopt=1,@Rowcount=@Rowcount OUTPUT
- SET @pageCount=CEILING(1.0*@Rowcount/@PageSize)--設置總頁數(shù)
- SELECT @pageCount
- AS 總頁數(shù),@Rowcount AS 總行數(shù),@CurrentPage AS 當前頁 --提示頁數(shù)
- IF(@CurrentPage>@pageCount)--如果傳入的當前頁碼大入總頁碼數(shù)則把當前頁數(shù)設為***一頁
- BEGIN
- SET @CurrentPage = @pageCount--設置當前頁碼數(shù)
- END
- IF(@CurrentPage<=0)--如果傳入的當前頁碼大入總頁碼數(shù)則把當前頁數(shù)設為***頁
- BEGIN
- SET @CurrentPage = 1--設置當前頁碼數(shù)
- END
- SET @CurrentPage=(@CurrentPage-1)*@PageSize+1 --設置當前頁碼數(shù)
- EXEC sp_cursorfetch @CursorId,16,@CurrentPage,@PageSize
- EXEC sp_cursorclose @CursorId --關閉游標
- SET NOCOUNT OFF
- END
#p#
Java調(diào)用儲存過程:
- package test;
- import java.sql.*;
- public class Study3 {
- private Connection con;
- public ResultSet rs;
- private CallableStatement callsta;
- private String use = "sa";
- private String pwd = "sa";
- public Study3() {
- try {
- // 連接數(shù)據(jù)庫驅動
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
- String str = "jdbc:microsoft:sqlserver://localhost:1433;databasename=test";
- con = DriverManager.getConnection(str, use, pwd);
- // 設置存儲過程參數(shù)
- String st = "{call Paging(?,?,?)}";
- callsta = con.prepareCall(st);
- callsta.setString(1, "select * from T_employee");
- callsta.setInt(2, 1);
- callsta.setInt(3, 3);
- // 循環(huán)輸出調(diào)用存儲過程的記錄結果
- StringBuffer sb=new StringBuffer();
- int rsNum=0;//統(tǒng)計結果集的數(shù)量
- int updateCount = -1;
- boolean flag = callsta.execute();// 這個而爾值只說明***個返回內(nèi)容是更新計數(shù)還是結果集。
- do {
- updateCount = callsta.getUpdateCount();
- if (updateCount != -1) {// 說明當前行是一個更新計數(shù)
- // 處理.
- System.out.println("..說明當前行是一個更新計數(shù)..");
- callsta.getMoreResults();
- continue;// 已經(jīng)是更新計數(shù)了,處理完成后應該移動到下一行
- // 不再判斷是否是ResultSet
- }
- rs = callsta.getResultSet();
- if (rs != null) {// 如果到了這里,說明updateCount == -1
- // 處理rs
- rsNum++;
- System.out.println("統(tǒng)計結果集的數(shù)量:"+rsNum);
- if (rs != null) {
- ResultSetMetaData rsmd = rs.getMetaData(); // 獲取字段名
- int numberOfColumns = rsmd.getColumnCount(); // 獲取字段數(shù)
- int i = 0;
- while (rs.next()) { // 將查詢結果取出
- for (i = 1; i <= numberOfColumns; i++) {
- // System.out.println(rs.getInt("總頁數(shù)"));
- String date = rs.getString(i);
- sb.append(date+" ");
- }
- }
- rs.close();
- }
- callsta.getMoreResults();
- continue;
- // 是結果集,處理完成后應該移動到下一行
- }
- // 如果到了這里,說明updateCount == -1 && rs == null,什么也沒的了
- System.out.println(sb.toString());
- } while (!(updateCount == -1 && rs == null));
- // callsta.getXXX(int);//獲取輸出參數(shù)
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] age) {
- Study3 study = new Study3();
- }
- }
原文鏈接:http://chenyunhong.iteye.com/blog/1096195
【編輯推薦】
責任編輯:艾婧
來源:
Jason的博客