Last active
February 16, 2017 12:45
-
-
Save fedulovivan/d78237d5d23459a3834795cc10c1a780 to your computer and use it in GitHub Desktop.
resultset optional colum reader implemented using generics and lambda, for java 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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