Java : Wrapper (JDBC) with Examples
Wrapper (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Wrapper methods.
Summary
Note :
- The code examples in this page use Apache Derby as the JDBC driver. Apache Derby was once included in older JDKs, but is not included in the latest JDK.
final var url = "jdbc:derby:memory:db;create=true";
try (final var connection = DriverManager.getConnection(url)) {
final var iface = EmbedConnection.class;
System.out.println("iface = " + iface);
if (connection.isWrapperFor(iface)) {
System.out.println("isWrapperFor = true");
final EmbedConnection unwrapped = connection.unwrap(iface);
System.out.println("unwrapped = " + unwrapped);
}
}
// Result
// ↓
//iface = class org.apache.derby.impl.jdbc.EmbedConnection
//isWrapperFor = true
//unwrapped = org.apache.derby.impl.jdbc.EmbedConnection@626393072
// (XID = 166), (SESSIONID = 1), (DATABASE = memory:db), (DRDAID = null)
Methods
boolean isWrapperFor (Class<?> iface)
final var url = "jdbc:derby:memory:db;create=true";
try (final var connection = DriverManager.getConnection(url)) {
final var iface = EmbedConnection.class;
System.out.println("iface = " + iface);
if (connection.isWrapperFor(iface)) {
System.out.println("isWrapperFor = true");
final EmbedConnection unwrapped = connection.unwrap(iface);
System.out.println("unwrapped = " + unwrapped);
}
}
// Result
// ↓
//iface = class org.apache.derby.impl.jdbc.EmbedConnection
//isWrapperFor = true
//unwrapped = org.apache.derby.impl.jdbc.EmbedConnection@626393072
// (XID = 166), (SESSIONID = 1), (DATABASE = memory:db), (DRDAID = null)
<T> T unwrap (Class<T> iface)
final var url = "jdbc:derby:memory:db;create=true";
try (final var connection = DriverManager.getConnection(url)) {
final var iface = EmbedConnection.class;
System.out.println("iface = " + iface);
if (connection.isWrapperFor(iface)) {
System.out.println("isWrapperFor = true");
final EmbedConnection unwrapped = connection.unwrap(iface);
System.out.println("unwrapped = " + unwrapped);
}
}
// Result
// ↓
//iface = class org.apache.derby.impl.jdbc.EmbedConnection
//isWrapperFor = true
//unwrapped = org.apache.derby.impl.jdbc.EmbedConnection@626393072
// (XID = 166), (SESSIONID = 1), (DATABASE = memory:db), (DRDAID = null)
Related posts
- API Examples