Java : Wrapper (JDBC) con ejemplos
Wrapper (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de Wrapper.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
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
- Ejemplos de API