All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleflatmapper.jdbc.impl.SelectQueryImpl Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 9.0.2
Show newest version
package org.simpleflatmapper.jdbc.impl;

import org.simpleflatmapper.jdbc.JdbcMapper;
import org.simpleflatmapper.jdbc.QueryPreparer;
import org.simpleflatmapper.jdbc.SelectQuery;
import org.simpleflatmapper.util.CheckedConsumer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectQueryImpl implements SelectQuery {
    private final QueryPreparer

queryPreparer; private final JdbcMapper mapper; public SelectQueryImpl(QueryPreparer

queryPreparer, JdbcMapper mapper) { this.queryPreparer = queryPreparer; this.mapper = mapper; } @Override public T readFirst(Connection connection, P p) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepare(connection).bind(p); try { ResultSet rs = preparedStatement.executeQuery(); try { if (rs.next()) { return mapper.map(rs); } return null; } finally { safeClose(rs); } } finally { safeClose(preparedStatement); } } @Override public > C read(Connection connection, P p, C consumer) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepare(connection).bind(p); try { ResultSet rs = preparedStatement.executeQuery(); try { mapper.forEach(rs, consumer); } finally { safeClose(rs); } } finally { safeClose(preparedStatement); } return consumer; } private void safeClose(PreparedStatement preparedStatement) { try { preparedStatement.close(); } catch (SQLException e) {} } private void safeClose(ResultSet rs) { try { rs.close(); } catch (SQLException e) {} } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy