Share via


unwrap Method (SQLServerCallableStatement)

Returns an object that implements the specified interface to allow access to the Microsoft SQL Server JDBC Driver-specific methods.

Note

This feature is introduced starting with the Microsoft SQL Server JDBC Driver version 2.0.

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

Parameters

iface

A class of type T defining an interface.

Return Value

An object that implements the specified interface.

Exceptions

SQLServerException

Remarks

The unwrap method is defined by the java.sql.Wrapper interface, which is introduced in the JDBC 4.0 Spec.

Applications might need to access extensions to the JDBC API that are specific to the Microsoft SQL Server JDBC Driver. The unwrap method supports unwrapping to public classes that this object extends, if the classes expose vendor extensions.

The SQLServerCallableStatement class extends the SQLServerPreparedStatement class, which is extended from the SQLServerStatement class. When this method is called, the object unwraps to the following classes: SQLServerStatement, SQLServerPreparedStatement, and SQLServerCallableStatement.

The following code example demonstrates how to use the isWrapperFor and unwrap methods to check the driver extensions and invoke the vendor-specific methods, such as setResponseBuffering and getResponseBuffering.

public static void executeStoredProcedure(Connection con) {
   try {
    CallableStatement cstmt = 
       con.prepareCall("{call dbo.stored_proc_name(?, ?)}");
    
    // The recommended way to access the Microsoft SQL Server 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();
  }
} 

See Also

Reference

isWrapperFor Method (SQLServerCallableStatement)
SQLServerCallableStatement Class

Concepts

SQLServerCallableStatement Methods
SQLServerCallableStatement Members