Skip to content

Instantly share code, notes, and snippets.

@fedulovivan
Last active February 16, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedulovivan/d78237d5d23459a3834795cc10c1a780 to your computer and use it in GitHub Desktop.
Save fedulovivan/d78237d5d23459a3834795cc10c1a780 to your computer and use it in GitHub Desktop.
resultset optional colum reader implemented using generics and lambda, for java 8
/**
* WARNINIG works with ojdbc7 only
*
* @param rs
* @param columnName
* @param columnType
* @param reader
* @throws SQLException
*/
protected static <T> void readOptionalColumn(
ResultSet rs,
String columnName,
Class<T> columnType,
Consumer<T> reader
) throws SQLException {
try {
reader.accept(rs.getObject(columnName, columnType));
} catch (SQLException e) {
String message = e.getMessage();
if (message == null) return;
String expectedOracleMessage = "Invalid column name";
String expectedPostgresMessage = String.format(
"The column name %s was not found",
columnName
);
if (!message.equals(expectedOracleMessage) && message.indexOf(expectedPostgresMessage) == -1) {
throw e;
}
}
}
//...
readOptionalColumn(rs, "comments", String.class, dt::setComments);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment