Wrappers and Interfaces

The Microsoft SQL Server JDBC Driver supports interfaces that allow you create a proxy of a class, and wrappers that let you access extensions to the JDBC API that are specific to the SQL Server JDBC Driver through a proxy interface.

Wrappers

The SQL Server JDBC Driver supports the java.sql.Wrapper interface. This interface provides a mechanism to access extensions to the JDBC API that are specific to the SQL Server JDBC Driver through a proxy interface.

The java.sql.Wrapper interface defines two methods: isWrapperFor and unwrap. The isWrapperFor method checks whether the specified input object implements this interface. The unwrap method returns an object that implements this interface to allow access to the SQL Server JDBC Driver specific methods.

isWrapperFor and unwrap methods are exposed as follows:

Interfaces

Beginning in SQL Server JDBC Driver 3.0, interfaces are available for an application server to access a driver specific method from the associated class. The application server can wrap the class by creating a proxy, exposing the SQL Server JDBC driver-specific functionality from an interface. The SQL Server JDBC driver supports interfaces that have the SQL Server JDBC driver specific methods and constants so an application server can create a proxy of the class.

The interfaces derive from standard Java interfaces so you can use the same object once it is unwrapped to access driver specific functionality or generic SQL Server JDBC driver functionality.

The following interfaces are added:

Example

Description

This sample demonstrates how to access to a SQL Server JDBC Driver-specific function from a DataSource object. This DataSource class may have been wrapped by an application server. To access the JDBC driver-specific function or constant, you can unwrap the datasource to an ISQLServerDataSource interface and use the functions declared in this interface.

Code

import javax.sql.*;
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;

public class UnWrapTest {
   public static void main(String[] args) {
      // This is a test.  This DataSource object could be something from an appserver 
      // which has wrapped the real SQLServerDataSource with its own wrapper
      SQLServerDataSource ds = new SQLServerDataSource();
      checkSendStringParametersAsUnicode(ds);
   }

   // Unwrap to the ISQLServerDataSource interface to access the getSendStringParametersAsUnicode function
   static void checkSendStringParametersAsUnicode(DataSource ds) {
      try {
         final ISQLServerDataSource sqlServerDataSource = ds.unwrap(ISQLServerDataSource.class);
         boolean sendStringParametersAsUnicode = sqlServerDataSource.getSendStringParametersAsUnicode();

         System.out.println("Send string as parameter value is:-" + sendStringParametersAsUnicode);

      } catch (SQLException sqlE) {
         System.out.println("Exception:-" + sqlE);
      }
   }
}

See Also

Other Resources

Understanding the JDBC Driver Data Types