Skip to content

Instantly share code, notes, and snippets.

@amuraco
Created December 3, 2012 16:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amuraco/4196137 to your computer and use it in GitHub Desktop.
Save amuraco/4196137 to your computer and use it in GitHub Desktop.
AbstractWrappedJDBC41Connection: JDBC 4.1 Compatibility with JDK 6+
package amuraco;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.concurrent.Executor;
public abstract class AbstractWrappedJDBC41Connection implements Connection {
protected Connection conn;
/** JDK7/JDBC4.1 compat **/
public void setSchema(String schema) throws SQLException {
try {
Method m = conn.getClass().getMethod("setSchema", String.class);
m.invoke(conn, schema);
} catch (NoSuchMethodException e) {
throw new SQLFeatureNotSupportedException("A JDK7/JDBC41 method was called but could not be found.",e);
} catch (Exception e) {
throw new SQLFeatureNotSupportedException(e);
}
}
public String getSchema() throws SQLException {
try {
Method m = conn.getClass().getMethod("getSchema");
return (String) m.invoke(conn);
} catch (NoSuchMethodException e) {
throw new SQLFeatureNotSupportedException("A JDK7/JDBC41 method was called but could not be found.",e);
} catch (Exception e) {
throw new SQLFeatureNotSupportedException(e);
}
}
public void abort(Executor executor) throws SQLException {
try {
Method m = conn.getClass().getMethod("abort",Executor.class);
m.invoke(conn,executor);
} catch (NoSuchMethodException e) {
throw new SQLFeatureNotSupportedException("A JDK7/JDBC41 method was called but could not be found.",e);
} catch (Exception e) {
throw new SQLFeatureNotSupportedException(e);
}
}
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
try {
Method m = conn.getClass().getMethod("setNetworkTimeout",Executor.class, int.class);
m.invoke(conn,executor,milliseconds);
} catch (NoSuchMethodException e) {
throw new SQLFeatureNotSupportedException("A JDK7/JDBC41 method was called but could not be found.",e);
} catch (Exception e) {
throw new SQLFeatureNotSupportedException(e);
}
}
public int getNetworkTimeout() throws SQLException {
try {
Method m = conn.getClass().getMethod("getNetworkTimeout");
return ((Integer) m.invoke(conn)).intValue();
} catch (NoSuchMethodException e) {
throw new SQLFeatureNotSupportedException("A JDK7/JDBC41 method was called but could not be found.",e);
} catch (Exception e) {
throw new SQLFeatureNotSupportedException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment