Skip to main content

JDBC 4.0 Wrapper Interface

Posted by lancea on February 23, 2006 at 2:46 PM PST

The Wrapper interface provides a mechanism for JDBC users to be able to access an instance of a resource which has been wrapped for architectural reasons. This mechanism helps to eliminate the need to use non-standard means to access vendor specific resources.

The following JDBC interfaces are subinterfaces of the Wrapper interface:

  • java.sql.Connection
  • java.sql.DatabaseMetaData
  • java.sql.ParameterMetaData
  • java.sql.ResultSet
  • java.sql.ResultSetMetaData
  • java.sql.Statement
  • javax.sql.Datasource

The Wrapper interface consists of two methods:

  • isWrapperFor - Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does.
  • unwrap - Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.

Here is a simple example of using these new methods:

  Statement stmt = conn.createStatement();
  Class clzz = Class.forName("oracle.jdbc.OracleStatement");
  OracleStatement os;
  if(stmt.isWrapperFor(clzz)) {
     os = stmt.unwrap(clzz);
     os.defineColumnType(1, Types.NUMBER);
Related Topics >>