Skip to content

Instantly share code, notes, and snippets.

@wpride
Created March 8, 2017 12:55
Show Gist options
  • Save wpride/7741538b940f64ede78cdffd5f9d3a7f to your computer and use it in GitHub Desktop.
Save wpride/7741538b940f64ede78cdffd5f9d3a7f to your computer and use it in GitHub Desktop.
Look up a value in Java
public String retrieveCacheValue(String entityKey, String cacheKey) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
preparedStatement = SqlHelper.prepareTableSelectStatement(connection,
TABLE_NAME,
new String[]{COL_CACHE_NAME, COL_ENTITY_KEY, COL_CACHE_KEY},
new String[]{mCacheName, entityKey, cacheKey});
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return resultSet.getString(resultSet.findColumn(COL_VALUE));
} else {
return null;
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
log.debug("Exception closing connection ", e);
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
log.debug("Exception closing prepared statement ", e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment