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

org.simpleflatmapper.jdbc.impl.MapperQueryBinder 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.QueryBinder;
import org.simpleflatmapper.util.ErrorHelper;

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

public class MapperQueryBinder implements QueryBinder {
    private final MapperQueryPreparer queryPreparer;
    private final Connection connection;

    public MapperQueryBinder(MapperQueryPreparer queryPreparer, Connection connection) {
        this.queryPreparer = queryPreparer;
        this.connection = connection;
    }

    @Override
    public PreparedStatement bind(T value) throws SQLException {
        PreparedStatement preparedStatement = queryPreparer.prepareStatement(connection);
        try {
            queryPreparer.mapper().mapTo(value, preparedStatement, null);
            return preparedStatement;
        } catch(Exception t) {
            try {
                preparedStatement.close();
            } catch(SQLException e) {
                // IGNORE
            }
            ErrorHelper.rethrow(t);
            return null;
        }
    }


    @Override
    public void bindTo(T value, PreparedStatement ps) throws SQLException {
        try {
            queryPreparer.mapper().mapTo(value, ps, null);
        } catch (Exception e) {
            ErrorHelper.rethrow(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy