try {
int returnGeneratedKeysEnumValue = Statement.class
.getDeclaredField( "RETURN_GENERATED_KEYS" )
.getInt( PreparedStatement.class );
RETURN_GENERATED_KEYS = new Integer( returnGeneratedKeysEnumValue );
PREPARE_STATEMENT_METHOD = Connection.class.getMethod(
"prepareStatement",
new Class[] { String.class, Integer.TYPE }
);
GET_GENERATED_KEYS_METHOD = Statement.class.getDeclaredMethod(
"getGeneratedKeys",
null
);
}
catch ( Exception e ) {
throw new AssertionFailure( "could not initialize getGeneratedKeys() support", e );
}
Object[] args = new Object[] { sql, RETURN_GENERATED_KEYS } ;
try {
return ( PreparedStatement ) PREPARE_STATEMENT_METHOD.invoke( conn, args );
}
catch ( InvocationTargetException ite ) {
if ( ite.getTargetException() instanceof SQLException ) {
throw ( SQLException ) ite.getTargetException();
}
else if ( ite.getTargetException() instanceof RuntimeException ) {
throw ( RuntimeException ) ite.getTargetException();
}
else {
throw new AssertionFailure( "InvocationTargetException preparing statement capable of returning generated keys (JDBC3)", ite );
}
}
catch ( IllegalAccessException iae ) {
throw new AssertionFailure( "IllegalAccessException preparing statement capable of returning generated keys (JDBC3)", iae );
}