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

net.dongliu.dbutils.QuerySQLContext Maven / Gradle / Ivy

package net.dongliu.dbutils;

import net.dongliu.commons.io.Closeables;
import net.dongliu.dbutils.exception.UncheckedSQLException;

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

/**
 * For query sql execute
 *
 * @author Liu Dong
 */
public class QuerySQLContext extends SingleSQLContext {

    /**
     * Executes the SQL query and returns result.
     */
    public SQLResultSet execute() {
        try {
            PreparedStatement statement = fetchSize == 0 ? connection.prepareStatement(clause) :
                    // mysql need to set those flags to make fetch size work
                    connection.prepareStatement(clause, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            try {
                fillStatement(statement, params());
                statement.setFetchSize(fetchSize);
                statement.execute();
                ResultSet rs = statement.getResultSet();
                if (closeConn) {
                    return new SQLResultSet(rs, statement, connection);
                } else {
                    return new SQLResultSet(rs, statement);
                }
            } catch (Throwable e) {
                Closeables.closeQuietly(statement);
                throw e;
            }
        } catch (SQLException e) {
            if (closeConn) {
                Closeables.closeQuietly(connection);
            }
            throw new UncheckedSQLException(e);
        } catch (Throwable t) {
            if (closeConn) {
                Closeables.closeQuietly(connection);
            }
            throw t;
        }
    }

    @Override
    protected QuerySQLContext self() {
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy