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 |
[!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.
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:
- isWrapperFor Method (SQLServerCallableStatement)
- unwrap Method (SQLServerCallableStatement)
- isWrapperFor Method (SQLServerConnectionPoolDataSource)
- unwrap Method (SQLServerConnectionPoolDataSource)
- isWrapperFor Method (SQLServerDataSource)
- unwrap Method (SQLServerDataSource)
- isWrapperFor Method (SQLServerPreparedStatement)
- unwrap Method (SQLServerPreparedStatement)
- isWrapperFor Method (SQLServerStatement)
- unwrap Method (SQLServerStatement)
- isWrapperFor Method (SQLServerXADataSource)
- unwrap Method (SQLServerXADataSource)
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:
- ISQLServerCallableStatement
- ISQLServerConnection
- ISQLServerDataSource
- ISQLServerPreparedStatement
- ISQLServerResultSet
- ISQLServerStatement
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.
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);
}
}
}