Skip to content

Instantly share code, notes, and snippets.

@niorko
Last active December 10, 2016 13:26
Show Gist options
  • Select an option

  • Save niorko/6a644fa6c0aa800a4bb91c09ef25cc5f to your computer and use it in GitHub Desktop.

Select an option

Save niorko/6a644fa6c0aa800a4bb91c09ef25cc5f to your computer and use it in GitHub Desktop.
/**
* Generic implementation of getting item from DB
*
* @param query SQL query
* @param mapper mapper that map {@link ResultSet} T object
* @param queryArguments callback for filling query arguments
* @param <T> POJO
* @return parsed instance of T
* @throws SQLException classic sql exception
* @throws IOException in case of uploading file
*/
public static <T> T getItem(
String query, Mapper<T> mapper,
QueryArguments queryArguments) throws SQLException, IOException {
T item = null;
try (Connection conn = ConnectionHelper.getConnection();
PreparedStatement statement = conn.prepareStatement(query)) {
conn.setAutoCommit(false);
// Put query arguments inside
if (queryArguments != null) {
queryArguments.put(statement);
}
try (OracleResultSet resultSet = (OracleResultSet) statement.executeQuery()) {
if (resultSet.next()) {
item = mapper.map(resultSet);
}
}
conn.commit();
}
return item;
}
// --------------------------------------
// Pooled connection je PooledConnection
@Nullable
public static Connection getConnection() throws SQLException {
return sPooledConnection.getConnection();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment