Java SQL ResultSet createLargeResultSetPreparedStatement(Connection conn, String sql)

Here you can find the source of createLargeResultSetPreparedStatement(Connection conn, String sql)

Description

Returns a new prepared statement on the specified connection with the necessary options to avoid running out of memory when reading a large result set.

License

Open Source License

Declaration

public static PreparedStatement createLargeResultSetPreparedStatement(Connection conn, String sql)
        throws SQLException 

Method Source Code

//package com.java2s;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Timestamp;

public class Main {
    /**/*from  w w  w .j a v a2 s .co m*/
     * Returns a new prepared statement on the specified connection with the necessary options
     * to avoid running out of memory when reading a large result set.
     */
    public static PreparedStatement createLargeResultSetPreparedStatement(Connection conn, String sql)
            throws SQLException {
        PreparedStatement st = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        st.setFetchSize(Integer.MIN_VALUE);
        return st;
    }

    public static PreparedStatement prepareStatement(Connection conn, String sql, Object... values)
            throws SQLException {
        PreparedStatement stmt = conn.prepareStatement(sql);
        setPreparedStatementValues(stmt, values);
        return stmt;
    }

    /**
     * Sets the given <code>values</code> on the prepared statement <code>st</code>. The values
     * are set on the statement starting at index 1 (the first index).
     *
     * @param values The values to set on the statement. May be <code>null</code> or empty.
     * @throws SQLException ff there is an error setting any prepared statement value.
     * @throws IllegalArgumentException if one of the objects in <code>values</code> is not
     * a recognized value type. The recognized types are instances of {@link String},
     * {@link Boolean}, {@link Byte}, {@link Short}, {@link Integer}, {@link Long},
     * {@link Float}, {@link Double}, {@link java.sql.Date}, {@link java.util.Date},
     * {@link java.sql.Timestamp}, and {@link Enum}.
     */
    @SuppressWarnings("unchecked")
    public static void setPreparedStatementValues(PreparedStatement st, Object... values) throws SQLException {
        if (values != null) {
            int index = 1;
            for (Object value : values) {
                if (value instanceof String) {
                    st.setString(index++, (String) value);
                } else if (value instanceof Boolean) {
                    st.setBoolean(index++, (Boolean) value);
                } else if (value instanceof Byte) {
                    st.setByte(index++, (Byte) value);
                } else if (value instanceof Short) {
                    st.setShort(index++, (Short) value);
                } else if (value instanceof Integer) {
                    st.setInt(index++, (Integer) value);
                } else if (value instanceof Long) {
                    st.setLong(index++, (Long) value);
                } else if (value instanceof Float) {
                    st.setFloat(index++, (Float) value);
                } else if (value instanceof Double) {
                    st.setDouble(index++, (Double) value);
                } else if (value instanceof Date) {
                    st.setDate(index++, (Date) value);
                } else if (value instanceof Timestamp) {
                    st.setTimestamp(index++, (Timestamp) value);
                } else if (value instanceof java.util.Date) {
                    st.setTimestamp(index++, new Timestamp(((java.util.Date) value).getTime()));
                } else if (value instanceof Enum) {
                    st.setString(index++, value.toString());
                } else {
                    throw new IllegalArgumentException(
                            "cannot handle object of type " + value.getClass().getName());
                }
            }
        }
    }
}

Related

  1. cleanupConnection(ResultSet rs, Statement stmt, Connection conn)
  2. cleanupJDBCResouce(ResultSet rs, Statement stmt, String oldCatalog, Connection conn)
  3. consume(ResultSet rs)
  4. consume2(ResultSet rs)
  5. copyInt(ResultSet rs, int sourcePos, PreparedStatement stmt, int destPos)
  6. createLargeResultSetStatement(Connection conn)
  7. createTable(ResultSet rs)
  8. createUid(final String[] keys, final ResultSet rs, final String keySeparator)
  9. encodeHeader(ResultSetMetaData metaData)