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:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);// w w  w.  ja  v  a 2 s .c  om
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int, name VARCHAR(30) );");

    String INSERT_RECORD = "select * from survey where id < ?";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);

    pstmt.setInt(1, 1);

    pstmt.setFetchSize(200);

    ResultSet rs = pstmt.executeQuery();

    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
}

From source file:edu.psu.citeseerx.disambiguation.CsxDisambiguation.java

public static void createBlocks(ListableBeanFactory factory) throws Exception {
    String dirpath = "data/csauthors/blocks";

    DataSource dataSource = (DataSource) factory.getBean("csxDataSource");

    PreparedStatement st = dataSource.getConnection().prepareStatement("SELECT * FROM authors",
            ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    st.setFetchSize(Integer.MIN_VALUE);
    ResultSet rs = st.executeQuery();

    initDirectories(dirpath);//from   w  ww  .  j  av a2s  .co m

    CsxAuthorFilter filter = (CsxAuthorFilter) factory.getBean("csxAuthorFilter");
    //new CsxAuthorFilter("data/csauthors/name_stopwords.txt");
    BufferedWriter skip = new BufferedWriter(new FileWriter("skip.txt"));

    int count = 0;
    Map<String, List<String>> blocks = new HashMap<String, List<String>>();
    while (rs.next()) {
        count++;
        if ((count % 10000) == 0)
            System.out.println("#Auth:" + count);
        String rsname = rs.getString("name");
        if (!filter.isStopword(rsname) && !filter.isInstitute(rsname) && !filter.isPosition(rsname)) {

            CsxAuthor auth = new CsxAuthor(rs);
            String lname = auth.getLastName();
            String fname = auth.getFirstName();

            if ((lname != null) && (fname != null)) {
                if ((lname.charAt(0) >= 'A') && (lname.charAt(0) <= 'Z') && (fname.charAt(0) >= 'A')
                        && (fname.charAt(0) <= 'Z') && !((fname.length() == 1) && (lname.length() == 1))
                        && !(lname.matches(".*/.*"))) {

                    String l_init = lname.substring(0, 1).toUpperCase();
                    String f_init = fname.substring(0, 1).toUpperCase();
                    String key = l_init + f_init + "/" + lname.toLowerCase() + "_" + f_init.toLowerCase()
                            + ".txt";

                    List<String> list;
                    if (!blocks.containsKey(key)) {
                        list = new ArrayList<String>();
                        blocks.put(key, list);
                    } else {
                        list = blocks.get(key);
                    }
                    list.add(auth.getId());
                } else {
                    skip.write("SKIP: [" + rsname + "]\n");
                }
            }
        }
    }
    skip.close();

    for (String key : blocks.keySet()) {
        List<String> aids = blocks.get(key);
        // only care about cluster with more than one document
        if (aids.size() > 1) {
            BufferedWriter out = new BufferedWriter(new FileWriter(dirpath + "/" + key));
            for (String aid : aids) {
                out.write(aid + "\n");
            }
            out.close();
        }
    }
}

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

public void deleteFile(final String name) throws IOException {
    jdbcTemplate.update(table.sqlMarkDeleteByName(), new PreparedStatementSetter() {
        @Override//  w w w  .j  ava  2s .co  m
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setFetchSize(1);
            ps.setBoolean(1, true);
            ps.setString(2, name);
        }
    });
}

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

public List deleteFiles(final List names) throws IOException {
    jdbcTemplate.batchUpdate(table.sqlDeleteByName(), new BatchPreparedStatementSetter() {
        @Override//from   w  ww.  j  a  va2 s.c o  m
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setFetchSize(1);
            ps.setString(1, (String) names.get(i));
        }

        @Override
        public int getBatchSize() {
            return names.size();
        }
    });
    return null;
}

From source file:dk.netarkivet.common.utils.DBUtils.java

/**
 * Prepare a statement for iteration given a query string, fetch size
 * and some args.//from   ww w  .j av a2  s . c o m
 *
 * NB: the provided connection is not closed.
 *
 * @param c a Database connection
 * @param fetchSize hint to JDBC driver on number of results to cache
 * @param query a query string  (must not be null or empty)
 * @param args some args to insert into this query string (must not be null)
 * @return a prepared statement
 * @throws SQLException If unable to prepare a statement
 * @throws ArgumentNotValid If unable to handle type of one the args, or
 * the arguments are either null or an empty String.
 */
public static PreparedStatement prepareStatement(Connection c, int fetchSize, String query, Object... args)
        throws SQLException {
    ArgumentNotValid.checkNotNull(c, "Connection c");
    ArgumentNotValid.checkPositive(fetchSize, "int fetchSize");
    ArgumentNotValid.checkNotNullOrEmpty(query, "String query");
    ArgumentNotValid.checkNotNull(args, "Object... args");
    c.setAutoCommit(false);
    PreparedStatement s = c.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    s.setFetchSize(fetchSize);
    int i = 1;
    for (Object arg : args) {
        if (arg instanceof String) {
            s.setString(i, (String) arg);
        } else if (arg instanceof Integer) {
            s.setInt(i, (Integer) arg);
        } else if (arg instanceof Long) {
            s.setLong(i, (Long) arg);
        } else if (arg instanceof Boolean) {
            s.setBoolean(i, (Boolean) arg);
        } else if (arg instanceof Date) {
            s.setTimestamp(i, new Timestamp(((Date) arg).getTime()));
        } else {
            throw new ArgumentNotValid("Cannot handle type '" + arg.getClass().getName()
                    + "'. We can only handle string, " + "int, long, date or boolean args for query: " + query);
        }
        i++;
    }
    return s;
}

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

public List deleteFiles(final List names) throws IOException {
    jdbcTemplate.batchUpdate(table.sqlMarkDeleteByName(), new BatchPreparedStatementSetter() {
        @Override/*from  ww  w . j  av a  2 s.c  o  m*/
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setFetchSize(1);
            ps.setBoolean(1, true);
            ps.setString(2, (String) names.get(i));
            ps.addBatch();
        }

        @Override
        public int getBatchSize() {
            return names.size();
        }
    });
    return null;
}

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

public void deleteFile(final String name) throws IOException {
    jdbcTemplate.execute(table.sqlDeleteByName(), new PreparedStatementCallback() {
        @Override/*from ww w.  j av  a  2  s .co  m*/
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.executeUpdate();
        }
    });
}

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

public void close() throws IOException {
    super.close();
    final long length = length();
    doBeforeClose();//from w w  w .  ja v a 2 s .  c o m
    jdbcDirectory.getJdbcTemplate().update(jdbcDirectory.getTable().sqlInsert(), new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            InputStream is = null;
            try {
                is = openInputStream();
                if (jdbcDirectory.getDialect().useInputStreamToInsertBlob()) {
                    ps.setBinaryStream(2, is, (int) length());
                } else {
                    ps.setBlob(2, new InputStreamBlob(is, length));
                }
                ps.setLong(3, length);
                ps.setBoolean(4, false);
            } catch (IOException e) {
                throw new SQLException(e);
            }
        }
    });
    doAfterClose();
}

From source file:com.javacreed.examples.spring.StreamingStatementCreator.java

@Override
public PreparedStatement createPreparedStatement(final Connection connection) throws SQLException {
    final PreparedStatement statement = connection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
            ResultSet.CONCUR_READ_ONLY);
    statement.setFetchSize(Integer.MIN_VALUE);
    return statement;
}

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

public void touchFile(final String name) throws IOException {
    jdbcTemplate.update(table.sqlUpdateLastModifiedByName(), new PreparedStatementCallback() {
        @Override/*from ww w  . j a  va2s  .  co m*/
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.executeUpdate();
        }
    });
}