自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

談談Java調(diào)用SQL Server分頁存儲過程

數(shù)據(jù)庫 SQL Server 后端
本文主要談談Java調(diào)用SQL Server分頁存儲的過程,其返回是多個結果集,只要呈現(xiàn)形式是代碼,文字不多,簡單易懂。

本文主要談談Java調(diào)用SQL Server分頁存儲的過程,其返回是多個結果集,只要呈現(xiàn)形式是代碼,文字不多,簡單易懂。

SQL存儲過程:

  1. USE [Db_8za8za_2]     
  2. GO     
  3.     
  4. SET ANSI_NULLS ON     
  5. GO     
  6. SET QUOTED_IDENTIFIER ON     
  7. GO     
  8. -- =============================================     
  9. -- Description:    <Description,,通用分頁存儲過程>     
  10. -- =============================================     
  11. ALTER PROCEDURE [dbo].[paging ]     
  12.     -- Add the parameters for the stored procedure here     
  13.     --傳入?yún)?shù)     
  14.     @SqlStr nvarchar(4000), --查詢字符串     
  15.     @CurrentPage int--第N頁(當前頁數(shù))     
  16.     @PageSize int --每頁行數(shù)     
  17. AS     
  18. BEGIN     
  19.     -- SET NOCOUNT ON added to prevent extra result sets from     
  20.     -- interfering with SELECT statements.     
  21.     SET NOCOUNT ON;     
  22.     --定義變量     
  23.     DECLARE @CursorId int --CursorId是游標的id     
  24.     DECLARE @Rowcount int --總記錄(行)數(shù)     
  25.     DECLARE @pageCount int --總頁數(shù)     
  26.     -- Insert statements for procedure here     
  27.         
  28.     EXEC sp_cursoropen @CursorId output,@SqlStr,     
  29.         @Scrollopt=1,@Ccopt=1,@Rowcount=@Rowcount OUTPUT     
  30.         
  31.     SET @pageCount=CEILING(1.0*@Rowcount/@PageSize)--設置總頁數(shù)     
  32.         
  33.     SELECT @pageCount     
  34.         AS 總頁數(shù),@Rowcount AS 總行數(shù),@CurrentPage AS 當前頁 --提示頁數(shù)     
  35.     
  36.     IF(@CurrentPage>@pageCount)--如果傳入的當前頁碼大入總頁碼數(shù)則把當前頁數(shù)設為***一頁     
  37.         BEGIN     
  38.             SET @CurrentPage = @pageCount--設置當前頁碼數(shù)     
  39.         END     
  40.     IF(@CurrentPage<=0)--如果傳入的當前頁碼大入總頁碼數(shù)則把當前頁數(shù)設為***頁     
  41.         BEGIN     
  42.             SET @CurrentPage = 1--設置當前頁碼數(shù)     
  43.         END     
  44.     SET @CurrentPage=(@CurrentPage-1)*@PageSize+1 --設置當前頁碼數(shù)     
  45.     
  46.     EXEC sp_cursorfetch @CursorId,16,@CurrentPage,@PageSize     
  47.     EXEC sp_cursorclose @CursorId    --關閉游標     
  48.     
  49.     SET NOCOUNT OFF     
  50. END   

#p#

Java調(diào)用儲存過程:

  1. package test;    
  2.     
  3. import java.sql.*;    
  4.     
  5. public class Study3 {    
  6.     private Connection con;    
  7.     public ResultSet rs;    
  8.     private CallableStatement callsta;    
  9.     private String use = "sa";    
  10.     private String pwd = "sa";    
  11.     
  12.     public Study3() {    
  13.         try {    
  14.             // 連接數(shù)據(jù)庫驅動    
  15.             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");    
  16.             String str = "jdbc:microsoft:sqlserver://localhost:1433;databasename=test";    
  17.             con = DriverManager.getConnection(str, use, pwd);    
  18.     
  19.             // 設置存儲過程參數(shù)    
  20.             String st = "{call Paging(?,?,?)}";    
  21.             callsta = con.prepareCall(st);    
  22.             callsta.setString(1"select * from T_employee");    
  23.             callsta.setInt(21);    
  24.             callsta.setInt(33);    
  25.     
  26.             // 循環(huán)輸出調(diào)用存儲過程的記錄結果    
  27.             StringBuffer sb=new StringBuffer();    
  28.             int rsNum=0;//統(tǒng)計結果集的數(shù)量    
  29.             int updateCount = -1;    
  30.             boolean flag = callsta.execute();// 這個而爾值只說明***個返回內(nèi)容是更新計數(shù)還是結果集。    
  31.             do {    
  32.                 updateCount = callsta.getUpdateCount();    
  33.                 if (updateCount != -1) {// 說明當前行是一個更新計數(shù)    
  34.                     // 處理.    
  35.                     System.out.println("..說明當前行是一個更新計數(shù)..");    
  36.                     callsta.getMoreResults();    
  37.                     continue;// 已經(jīng)是更新計數(shù)了,處理完成后應該移動到下一行    
  38.                     // 不再判斷是否是ResultSet    
  39.                 }    
  40.                 rs = callsta.getResultSet();    
  41.                 if (rs != null) {// 如果到了這里,說明updateCount == -1    
  42.                     // 處理rs    
  43.                     rsNum++;    
  44.                     System.out.println("統(tǒng)計結果集的數(shù)量:"+rsNum);    
  45.                     if (rs != null) {    
  46.                         ResultSetMetaData rsmd = rs.getMetaData(); // 獲取字段名    
  47.                         int numberOfColumns = rsmd.getColumnCount(); // 獲取字段數(shù)    
  48.                         int i = 0;    
  49.                         while (rs.next()) { // 將查詢結果取出    
  50.                             for (i = 1; i <= numberOfColumns; i++) {    
  51. //                              System.out.println(rs.getInt("總頁數(shù)"));    
  52.                                 String date = rs.getString(i);    
  53.                                 sb.append(date+" ");    
  54.                             }    
  55.                         }    
  56.                         rs.close();    
  57.                     }    
  58.                     callsta.getMoreResults();    
  59.                     continue;    
  60.                     // 是結果集,處理完成后應該移動到下一行    
  61.                 }    
  62.                 // 如果到了這里,說明updateCount == -1 && rs == null,什么也沒的了    
  63.                 System.out.println(sb.toString());    
  64.             } while (!(updateCount == -1 && rs == null));    
  65.             // callsta.getXXX(int);//獲取輸出參數(shù)    
  66.         } catch (Exception e) {    
  67.             e.printStackTrace();    
  68.         }    
  69.     }    
  70.     
  71.     public static void main(String[] age) {    
  72.         Study3 study = new Study3();    
  73.     }    
  74. }   

原文鏈接:http://chenyunhong.iteye.com/blog/1096195

【編輯推薦】

  1. 說說Top子句對查詢計劃的影響
  2. SQL Server復災 你懂了嗎?
  3. SQL Server管理 這些你懂嗎?
  4. 用一句SQL解決SQL中斷號問題
  5. 如何用Java操作MongoDB
責任編輯:艾婧 來源: Jason的博客
相關推薦

2010-11-10 15:16:14

Sql Server分

2011-03-28 10:46:36

sql server存儲分頁

2010-11-12 09:46:55

Sql Server存

2011-03-24 13:38:47

SQL Server 存儲分頁

2010-09-14 10:47:45

sql server存

2012-05-10 11:17:23

JavaSQL Server

2011-08-16 16:59:58

PLSQL分頁存儲過程Java

2010-06-30 14:36:49

SQL Server

2010-06-18 10:34:38

SQL Server

2010-01-22 16:48:54

VB.NET調(diào)用SQL

2011-08-29 10:55:03

SQL Server分頁存儲過程優(yōu)化效率分

2015-08-19 14:18:56

SQLasp.net后臺調(diào)用

2011-08-22 10:15:39

數(shù)據(jù)庫存儲過程

2010-09-03 15:08:03

SQLselect語句

2009-08-06 16:44:06

2010-07-15 12:38:14

SQL Server存

2010-07-26 14:43:31

SQL Server存

2011-09-01 13:43:23

VC調(diào)用SQL Ser

2011-08-29 15:52:19

SQL ServerMybatis存儲過程

2010-09-14 10:36:23

sql server存
點贊
收藏

51CTO技術棧公眾號