Last active
December 10, 2016 13:26
-
-
Save niorko/6a644fa6c0aa800a4bb91c09ef25cc5f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * 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