Skip to content

Latest commit

 

History

History
92 lines (68 loc) · 5.55 KB

wrappers-and-interfaces.md

File metadata and controls

92 lines (68 loc) · 5.55 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic
Wrappers and interfaces
Learn how to create proxy interfaces and wrappers that let you access extensions to the JDBC API.
David-Engel
davidengel
08/12/2019
sql
connectivity
conceptual

Wrappers and interfaces

[!INCLUDEDriver_JDBC_Download]

The [!INCLUDEjdbcNoVersion] 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 [!INCLUDEjdbcNoVersion] through a proxy interface.

Wrappers

The [!INCLUDEjdbcNoVersion] supports the java.sql.Wrapper interface. This interface provides a mechanism to access extensions to the JDBC API that are specific to the [!INCLUDEjdbcNoVersion] 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 [!INCLUDEjdbcNoVersion] specific methods.

isWrapperFor and unwrap methods are exposed as follows:

Interfaces

Beginning in [!INCLUDEssNoVersion] 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 [!INCLUDEjdbcNoVersion]-specific functionality from an interface. The [!INCLUDEjdbcNoVersion] supports interfaces that have the [!INCLUDEjdbcNoVersion] 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 [!INCLUDEjdbcNoVersion] functionality.

The following interfaces are added:

Example

Description

This sample demonstrates how to access to a [!INCLUDEjdbcNoVersion]-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

Understanding the JDBC driver data types