unwrap 方法 (SQLServerCallableStatement)

下載 JDBC 驅動程式

傳回可實作所指定介面的物件,此介面可用來存取適用於 SQL Server 的 Microsoft JDBC 驅動程式特有的方法。

語法

  
public <T> T unwrap(Class<T> iface)  

參數

iface

T 類型的類別,可定義介面。

傳回值

物件,可實作指定的介面。

例外狀況

SQLServerException

備註

unwrap 方法是由 JDBC 4.0 規格中引進的 java.sql.Wrapper 介面所定義。

應用程式可能需要存取適用於 SQL Server 的 Microsoft JDBC 驅動程式特有的 JDBC API 延伸模組。 在類別公開供應商延伸模組的情況下,unwrap 方法支援解除包裝為這個物件可擴充的公用類別。

SQLServerCallableStatement 會實作 ISQLServerPreparedStatement,後者是從 ISQLServerStatement 擴充而來。 當呼叫這個方法時,該物件會解除包裝成為下列類別:SQLServerStatementSQLServerPreparedStatementSQLServerCallableStatement

如需詳細資訊,請參閱包裝函式與介面

下面範例程式碼會示範如何使用 isWrapperFor 和 unwrap 方法來核取驅動程式延伸模組,以及叫用供應商特定方法,例如 setResponseBufferinggetResponseBuffering

public static void executeStoredProcedure(Connection con) {  
   try {  
    CallableStatement cstmt =   
       con.prepareCall("{call dbo.stored_proc_name(?, ?)}");  
  
    // The recommended way to access the JDBC   
    // Driver-specific methods is to use the JDBC 4.0 Wrapper   
    // functionality.   
    // The following code statements demonstrates how to use the   
    // isWrapperFor and unwrap methods  
    // to access the driver-specific response buffering methods.  
  
    if (cstmt.isWrapperFor(  
      com.microsoft.sqlserver.jdbc.SQLServerCallableStatement.class)) {  
     // The CallableStatement object can unwrap to   
     // SQLServerCallableStatement.  
     SQLServerCallableStatement SQLcstmt =   
     cstmt.unwrap(  
        com.microsoft.sqlserver.jdbc.SQLServerCallableStatement.class);  
     SQLcstmt.setResponseBuffering("adaptive");  
     System.out.println("Response buffering mode has been set to " +  
         SQLcstmt.getResponseBuffering());  
     }  
  
    if (cstmt.isWrapperFor(  
      com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.class)) {  
      // The CallableStatement object can unwrap to   
      // SQLServerPreparedStatement.                    
      SQLServerPreparedStatement SQLpstmt =   
       cstmt.unwrap(  
       com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.class);  
      SQLpstmt.setResponseBuffering("adaptive");  
      System.out.println("Response buffering mode has been set to " +  
          SQLpstmt.getResponseBuffering());  
    }  
    if (cstmt.isWrapperFor(  
      com.microsoft.sqlserver.jdbc.SQLServerStatement.class)) {  
  
      // The CallableStatement object can unwrap to SQLServerStatement.   
      SQLServerStatement SQLstmt =   
        cstmt.unwrap(  
        com.microsoft.sqlserver.jdbc.SQLServerStatement.class);  
      SQLstmt.setResponseBuffering("adaptive");  
      System.out.println("Response buffering mode has been set to " +  
      SQLstmt.getResponseBuffering());  
    }  
  }  
  catch (Exception e) {  
     e.printStackTrace();  
  }  
}   

另請參閱

isWrapperFor 方法 (SQLServerCallableStatement)
SQLServerCallableStatement 成員
SQLServerCallableStatement 類別