org.simpleflatmapper.jdbc.impl.MapperQueryBinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-jdbc Show documentation
Show all versions of sfm-jdbc Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
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);
}
}
}