Example usage for java.sql PreparedStatement setFetchSize

List of usage examples for java.sql PreparedStatement setFetchSize

Introduction

In this page you can find the example usage for java.sql PreparedStatement setFetchSize.

Prototype

void setFetchSize(int rows) throws SQLException;

Source Link

Document

Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects generated by this Statement.

Usage

From source file:org.apache.lucene.store.jdbc.handler.AbstractFileEntryHandler.java

public boolean fileExists(final String name) throws IOException {
    return ((Boolean) jdbcTemplate.execute(table.sqlSelectNameExists(), new PreparedStatementCallback() {

        @Override/*from  www . java  2 s.  co  m*/
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.execute();
        }
    })).booleanValue();
}

From source file:org.apache.lucene.store.jdbc.handler.AbstractFileEntryHandler.java

public long fileModified(final String name) throws IOException {
    return ((Long) jdbcTemplate.execute(table.sqlSelecltLastModifiedByName(), new PreparedStatementCallback() {

        @Override/*from   ww  w. j  ava 2  s .c  om*/
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.execute();
        }
    })).longValue();
}

From source file:org.apache.lucene.store.jdbc.handler.AbstractFileEntryHandler.java

public void renameFile(final String from, final String to) throws IOException {
    // TODO find a way if it can be done in the same sql query
    deleteFile(to);//ww  w  .ja  v  a 2  s.  com
    jdbcTemplate.update(table.sqlUpdateNameByName(), new PreparedStatementCallback() {
        @Override
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, to);
            ps.setString(2, from);
            return ps.executeUpdate();
        }
    });
}

From source file:org.apache.lucene.store.jdbc.handler.AbstractFileEntryHandler.java

public long fileLength(final String name) throws IOException {
    return ((Long) jdbcTemplate.execute(table.sqlSelectSizeByName(), new PreparedStatementCallback() {
        @Override//from w ww.ja  va 2s  .  c om
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);

            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return new Long(rs.getLong(1));
            }
            return new Long(0L);
        }
    })).longValue();
}

From source file:org.apache.lucene.store.jdbc.index.FetchOnOpenJdbcIndexInput.java

public void configure(final String name, final JdbcDirectory jdbcDirectory, JdbcFileEntrySettings settings)
        throws IOException {
    jdbcDirectory.getJdbcTemplate().execute(jdbcDirectory.getTable().sqlSelectSizeValueByName(),
            new PreparedStatementCallback<Object>() {
                @Override//w  ww.  j a  v a  2  s.  com
                public Object doInPreparedStatement(PreparedStatement ps)
                        throws SQLException, DataAccessException {
                    ps.setFetchSize(1);
                    ps.setString(1, name);
                    try (ResultSet rs = ps.executeQuery()) {
                        if (!rs.next()) {
                            throw new JdbcStoreException(
                                    "No entry for [" + name + "] table " + jdbcDirectory.getTable());
                        }
                        length = rs.getInt(3);

                        Blob blob = rs.getBlob(2);
                        data = blob.getBytes(1, (int) length);
                        if (data.length != length) {
                            throw new IOException("read past EOF");
                        }

                    } catch (JdbcStoreException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
}

From source file:org.dcache.chimera.DirectoryStreamImpl.java

DirectoryStreamImpl(FsInode dir, JdbcTemplate jdbc) {
    _jdbc = jdbc;//from   w  w  w.jav  a2  s.  c o  m

    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs;
    try {
        connection = DataSourceUtils.getConnection(_jdbc.getDataSource());
        ps = connection.prepareStatement(QUERY);
        ps.setFetchSize(50);
        ps.setLong(1, dir.ino());
        ps.setLong(2, dir.ino());
        ps.setLong(3, dir.ino());
        rs = ps.executeQuery();
    } catch (SQLException ex) {
        JdbcUtils.closeStatement(ps);
        DataSourceUtils.releaseConnection(connection, _jdbc.getDataSource());
        throw _jdbc.getExceptionTranslator().translate("StatementExecution", QUERY, ex);
    }
    _connection = connection;
    _resultSet = rs;
    _statement = ps;
}

From source file:org.apache.lucene.store.jdbc.index.FetchOnBufferReadJdbcIndexInput.java

protected synchronized void readInternal(final byte[] b, final int offset, final int length)
        throws IOException {
    jdbcDirectory.getJdbcTemplate().execute(jdbcDirectory.getTable().sqlSelectSizeValueByName(),
            new PreparedStatementCallback<Object>() {
                @Override// w w w .j a v  a  2  s .co m
                public Object doInPreparedStatement(PreparedStatement ps)
                        throws SQLException, DataAccessException {
                    ps.setFetchSize(1);
                    ps.setString(1, name);
                    try (ResultSet rs = ps.executeQuery()) {
                        if (!rs.next()) {
                            throw new JdbcStoreException(
                                    "No entry for [" + name + "] table " + jdbcDirectory.getTable());
                        }
                        Blob blob = rs.getBlob(2);
                        readInternal(blob, b, offset, length);
                        synchronized (this) {
                            if (FetchOnBufferReadJdbcIndexInput.this.totalLength == -1) {
                                FetchOnBufferReadJdbcIndexInput.this.totalLength = rs.getLong(3);
                            }
                        }
                    } catch (JdbcStoreException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
}

From source file:org.apache.lucene.store.jdbc.index.FetchOnBufferReadJdbcIndexInput.java

protected void refill() throws IOException {
    jdbcDirectory.getJdbcTemplate().execute(jdbcDirectory.getTable().sqlSelectSizeValueByName(),
            new PreparedStatementCallback<Object>() {
                @Override//from   w w  w.j ava  2 s  . com
                public Object doInPreparedStatement(PreparedStatement ps)
                        throws SQLException, DataAccessException {
                    ps.setFetchSize(1);
                    ps.setString(1, name);
                    try (ResultSet rs = ps.executeQuery()) {
                        // START read blob and update length if required
                        if (!rs.next()) {
                            throw new JdbcStoreException(
                                    "No entry for [" + name + "] table " + jdbcDirectory.getTable());
                        }
                        synchronized (this) {
                            if (totalLength == -1) {
                                totalLength = rs.getLong(3);
                            }
                        }
                        // END read blob and update length if required

                        long start = bufferStart + bufferPosition;
                        long end = start + bufferSize;
                        if (end > length()) // don't read past EOF
                            end = length();
                        bufferLength = (int) (end - start);
                        if (bufferLength <= 0)
                            throw new IOException("read past EOF");

                        if (buffer == null) {
                            buffer = new byte[bufferSize]; // allocate buffer lazily
                            seekInternal(bufferStart);
                        }
                        // START replace read internal
                        Blob blob = rs.getBlob(2);
                        readInternal(blob, buffer, 0, bufferLength);

                        bufferStart = start;
                        bufferPosition = 0;
                    } catch (JdbcStoreException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
}

From source file:net.solarnetwork.node.dao.jdbc.AbstractJdbcDatumDao.java

/**
 * Find datum entities that have not been uploaded to a specific
 * destination./*  w w w  . ja  va 2  s.  c o m*/
 * 
 * <p>
 * This executes SQL from the {@code findForUploadSql} property. It uses the
 * {@code maxFetchForUpload} property to limit the number of rows returned,
 * so the call may not return all rows available from the database (this is
 * to conserve memory and process the data in small batches).
 * </p>
 * 
 * @param destination
 *        the destination to look for
 * @param rowMapper
 *        a {@link RowMapper} implementation to instantiate entities from
 *        found rows
 * @return the matching rows, never <em>null</em>
 */
protected List<T> findDatumNotUploaded(final RowMapper<T> rowMapper) {
    List<T> result = getJdbcTemplate().query(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String sql = getSqlResource(SQL_RESOURCE_FIND_FOR_UPLOAD);
            if (log.isTraceEnabled()) {
                log.trace("Preparing SQL to find datum not uploaded [" + sql + "] with maxFetchForUpload ["
                        + maxFetchForUpload + ']');
            }
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setFetchDirection(ResultSet.FETCH_FORWARD);
            ps.setFetchSize(maxFetchForUpload);
            ps.setMaxRows(maxFetchForUpload);
            return ps;
        }
    }, rowMapper);
    if (log.isDebugEnabled()) {
        log.debug("Found " + result.size() + " datum entities not uploaded");
    }
    return result;
}

From source file:de.ufinke.cubaja.sql.Database.java

/**
 * Creates a <tt>Query</tt> instance with SQL provided as <tt>Sql</tt> object.
 * @param sql//from   w ww .  j a  v  a 2s .c  om
 * @return Query
 * @throws SQLException
 */
public Query createQuery(Sql sql) throws SQLException {

    String stm = sql.getSingleStatement();

    if (logger != null) {
        logger.debug(text.get("prepare", myId, stm));
    }

    PreparedStatement ps = connection.prepareStatement(stm);
    ps.setFetchSize(config.getFetchSize());
    return new Query(ps, sql, config);
}