返回一个实现指定接口的对象,从而允许访问特定于 Microsoft SQL Server JDBC Driver 的方法。
注意:
从 Microsoft SQL Server JDBC Driver 2.0 版开始引入此功能。
语法
public <T> T unwrap(Class<T> iface)
参数
iface
定义接口的类型为 T 的类。
返回值
实现指定接口的对象。
异常
备注
unwrap 方法由在 JDBC 4.0 规范中引入的 java.sql.Wrapper 接口定义。
应用程序可能需要访问特定于 Microsoft SQL Server JDBC Driver 的 JDBC API 扩展。如果类公开供应商扩展插件,则 unwrap 方法支持对此对象扩展的公共类取消包装。
SQLServerCallableStatement 实现了 ISQLServerPreparedStatement,而后者则是从 ISQLServerStatement 扩展出来的。调用此方法时,对象会取消对下列类的包装:SQLServerStatement、SQLServerPreparedStatement 和 SQLServerCallableStatement。
有关详细信息,请参阅包装和接口。
下面的代码示例演示了如何使用 isWrapperFor 和 unwrap 方法检查驱动程序扩展插件和调用供应商特定方法,例如 setResponseBuffering 和 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(); } }