前一段时间我用了下面的咒语。这些方法是一个更大类的摘录。
import com.mchange.v2.c3p0.C3P0ProxyStatement;
import oracle.jdbc.OraclePreparedStatement;
static {
try {
SET_FORM_OF_USE_METHOD = OraclePreparedStatement.class.getDeclaredMethod("setFormOfUse", new Class[] { Integer.TYPE, Short.TYPE });
} catch (NoSuchMethodException ex) {
LOG.fatal("Can't find the setFormOfUse method", ex);
throw new ExceptionInInitializerError(ex);
}
}
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws SQLException {
if (st instanceof OraclePreparedStatement) {
((OraclePreparedStatement)st).setFormOfUse(index, OraclePreparedStatement.FORM_NCHAR);
} else if (st instanceof C3P0ProxyStatement) {
try {
C3P0ProxyStatement c3p0St = (C3P0ProxyStatement) st;
c3p0St.rawStatementOperation(SET_FORM_OF_USE_METHOD, C3P0ProxyStatement.RAW_STATEMENT, new Object[]{index, OraclePreparedStatement.FORM_NCHAR});
} catch (IllegalAccessException ex) {
throw new UnexpectedException("Error calling setFormOfUse through C3P0", ex);
} catch (IllegalArgumentException ex) {
throw new UnexpectedException("Error calling setFormOfUse through C3P0", ex);
} catch (InvocationTargetException ex) {
throw new UnexpectedException("Error calling setFormOfUse through C3P0", ex);
}
} else {
throw new IllegalArgumentException("Unkown PreparedStatement implementation: " + st.getClass() + ". Maybe an unknown connection pool is hiding the OraclePreparedStatement?");
}
st.setString(index, (String) value);
}
nullSafeSet方法的结构是直接在OraclePreparedStatement实例上工作,或者在拥有C3P0连接池的情况下在C3P0ProxyStatement上工作;对于不同的池,必须使用其他选项。
此方法适用于Oracle 10.2.0.4中的ojdbc14.jar。