org.itsallcode.jdbc.SimplePreparedStatement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simple-jdbc Show documentation
Show all versions of simple-jdbc Show documentation
Library to simplify using JDBC
package org.itsallcode.jdbc;
import java.sql.*;
import org.itsallcode.jdbc.dialect.DbDialect;
import org.itsallcode.jdbc.resultset.*;
class SimplePreparedStatement implements AutoCloseable {
private final Context context;
private final DbDialect dialect;
private final PreparedStatement statement;
private final String sql;
SimplePreparedStatement(final Context context, final DbDialect dialect, final PreparedStatement statement,
final String sql) {
this.context = context;
this.dialect = dialect;
this.statement = statement;
this.sql = sql;
}
SimpleResultSet executeQuery(final ContextRowMapper rowMapper) {
final ResultSet resultSet = doExecute();
final ResultSet convertingResultSet = ConvertingResultSet.create(dialect, resultSet);
return new SimpleResultSet<>(context, convertingResultSet, rowMapper);
}
private ResultSet doExecute() {
try {
return statement.executeQuery();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error executing query '" + sql + "'", e);
}
}
@Override
public void close() {
try {
statement.close();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error closing statement", e);
}
}
void setValues(final PreparedStatementSetter preparedStatementSetter) {
try {
preparedStatementSetter.setValues(statement);
} catch (final SQLException e) {
throw new UncheckedSQLException("Error setting values for prepared statement", e);
}
}
void executeBatch() {
try {
statement.executeBatch();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error executing batch sql '" + sql + "'", e);
}
}
void addBatch() {
try {
this.statement.addBatch();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error adding batch", e);
}
}
SimpleParameterMetaData getParameterMetadata() {
try {
return SimpleParameterMetaData.create(statement.getParameterMetaData());
} catch (final SQLException e) {
throw new UncheckedSQLException("Error getting parameter metadata", e);
}
}
}