org.springframework.jdbc.support.lob

## Interface LobHandler

- All Known Implementing Classes:
    AbstractLobHandler, DefaultLobHandler, OracleLobHandler

      public interface LobHandler

  Abstraction for handling large binary fields and large text fields in specific databases, no matter if represented as simple types or Large OBjects. Its main purpose is to isolate Oracle's peculiar handling of LOBs in `OracleLobHandler`; most other databases should be able to work with the provided `DefaultLobHandler`.

  Provides accessor methods for BLOBs and CLOBs, and acts as factory for LobCreator instances, to be used as sessions for creating BLOBs or CLOBs. LobCreators are typically instantiated for each statement execution or for each transaction; they are not thread-safe because they might track allocated database resources in order to free them after execution.

  Most databases/drivers should be able to work with `DefaultLobHandler`, which by default delegates to JDBC's direct accessor methods, avoiding the `java.sql.Blob` and `java.sql.Clob` API completely. `DefaultLobHandler` can also be configured to access LOBs using `PreparedStatement.setBlob/setClob` (e.g. for PostgreSQL), through setting the `"wrapAsLob"` property.

  Unfortunately, Oracle 9i just accepts Blob/Clob instances created via its own proprietary BLOB/CLOB API, and additionally doesn't accept large streams for PreparedStatement's corresponding setter methods. Therefore, you need to use `OracleLobHandler` there, which uses Oracle's BLOB/CLOB API for both types of access. The Oracle 10g JDBC driver should basically work with `DefaultLobHandler` as well, with some limitations in terms of LOB sizes.

  Of course, you need to declare different field types for each database. In Oracle, any binary content needs to go into a BLOB, and all character content beyond 4000 bytes needs to go into a CLOB. In MySQL, there is no notion of a CLOB type but rather a LONGTEXT type that behaves like a VARCHAR. For complete portability, use a LobHandler for fields that might typically require LOBs on some database because of the field size (take Oracle's numbers as a guideline).

  Summarizing the recommended options (for actual LOB fields):

  - JDBC 4.0 driver: `DefaultLobHandler` with `streamAsLob=true`.
  - PostgreSQL: `DefaultLobHandler` with `wrapAsLob=true`.
  - Oracle 9i/10g: `OracleLobHandler` with a connection-pool-specific `NativeJdbcExtractor`.
  - For all other database drivers (and for non-LOB fields that might potentially turn into LOBs on some databases): a plain `DefaultLobHandler`.
  Since:
    23.12.2003
  Author:
    Juergen Hoeller
  See Also:
    `DefaultLobHandler`, `OracleLobHandler`, `ResultSet.getBlob(int)`, `ResultSet.getClob(int)`, `ResultSet.getBytes(int)`, `ResultSet.getBinaryStream(int)`, `ResultSet.getString(int)`, `ResultSet.getAsciiStream(int)`, `ResultSet.getCharacterStream(int)`

- - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `InputStream` | `getBlobAsBinaryStream(ResultSet rs, int columnIndex)` Retrieve the given column as binary stream from the given ResultSet.
    `InputStream` | `getBlobAsBinaryStream(ResultSet rs, String columnName)` Retrieve the given column as binary stream from the given ResultSet.
    `byte[]` | `getBlobAsBytes(ResultSet rs, int columnIndex)` Retrieve the given column as bytes from the given ResultSet.
    `byte[]` | `getBlobAsBytes(ResultSet rs, String columnName)` Retrieve the given column as bytes from the given ResultSet.
    `InputStream` | `getClobAsAsciiStream(ResultSet rs, int columnIndex)` Retrieve the given column as ASCII stream from the given ResultSet.
    `InputStream` | `getClobAsAsciiStream(ResultSet rs, String columnName)` Retrieve the given column as ASCII stream from the given ResultSet.
    `Reader` | `getClobAsCharacterStream(ResultSet rs, int columnIndex)` Retrieve the given column as character stream from the given ResultSet.
    `Reader` | `getClobAsCharacterStream(ResultSet rs, String columnName)` Retrieve the given column as character stream from the given ResultSet.
    `String` | `getClobAsString(ResultSet rs, int columnIndex)` Retrieve the given column as String from the given ResultSet.
    `String` | `getClobAsString(ResultSet rs, String columnName)` Retrieve the given column as String from the given ResultSet.
    `LobCreator` | `getLobCreator()` Create a new `LobCreator` instance, i.e.

- - ### Method Detail

    - #### getBlobAsBytes

          byte[] getBlobAsBytes(ResultSet rs,
                              String columnName)
                                throws SQLException

      Retrieve the given column as bytes from the given ResultSet. Might simply invoke `ResultSet.getBytes` or work with `ResultSet.getBlob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnName` - the column name to use
      Returns:
        the content as byte array, or `null` in case of SQL NULL
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getBytes(int)`

    - #### getBlobAsBytes

          byte[] getBlobAsBytes(ResultSet rs,
                              int columnIndex)
                                throws SQLException

      Retrieve the given column as bytes from the given ResultSet. Might simply invoke `ResultSet.getBytes` or work with `ResultSet.getBlob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnIndex` - the column index to use
      Returns:
        the content as byte array, or `null` in case of SQL NULL
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getBytes(int)`

    - #### getBlobAsBinaryStream

          InputStream getBlobAsBinaryStream(ResultSet rs,
                                          String columnName)
                                            throws SQLException

      Retrieve the given column as binary stream from the given ResultSet. Might simply invoke `ResultSet.getBinaryStream` or work with `ResultSet.getBlob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnName` - the column name to use
      Returns:
        the content as binary stream, or `null` in case of SQL NULL
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getBinaryStream(int)`

    - #### getBlobAsBinaryStream

          InputStream getBlobAsBinaryStream(ResultSet rs,
                                          int columnIndex)
                                            throws SQLException

      Retrieve the given column as binary stream from the given ResultSet. Might simply invoke `ResultSet.getBinaryStream` or work with `ResultSet.getBlob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnIndex` - the column index to use
      Returns:
        the content as binary stream, or `null` in case of SQL NULL
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getBinaryStream(int)`

    - #### getClobAsString

          String getClobAsString(ResultSet rs,
                               String columnName)
                                 throws SQLException

      Retrieve the given column as String from the given ResultSet. Might simply invoke `ResultSet.getString` or work with `ResultSet.getClob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnName` - the column name to use
      Returns:
        the content as String, or `null` in case of SQL NULL
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getString(int)`

    - #### getClobAsString

          String getClobAsString(ResultSet rs,
                               int columnIndex)
                                 throws SQLException

      Retrieve the given column as String from the given ResultSet. Might simply invoke `ResultSet.getString` or work with `ResultSet.getClob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnIndex` - the column index to use
      Returns:
        the content as String, or `null` in case of SQL NULL
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getString(int)`

    - #### getClobAsAsciiStream

          InputStream getClobAsAsciiStream(ResultSet rs,
                                         String columnName)
                                           throws SQLException

      Retrieve the given column as ASCII stream from the given ResultSet. Might simply invoke `ResultSet.getAsciiStream` or work with `ResultSet.getClob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnName` - the column name to use
      Returns:
        the content as ASCII stream, or `null` in case of SQL NULL
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getAsciiStream(int)`

    - #### getClobAsAsciiStream

          InputStream getClobAsAsciiStream(ResultSet rs,
                                         int columnIndex)
                                           throws SQLException

      Retrieve the given column as ASCII stream from the given ResultSet. Might simply invoke `ResultSet.getAsciiStream` or work with `ResultSet.getClob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnIndex` - the column index to use
      Returns:
        the content as ASCII stream, or `null` in case of SQL NULL
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getAsciiStream(int)`

    - #### getClobAsCharacterStream

          Reader getClobAsCharacterStream(ResultSet rs,
                                        String columnName)
                                          throws SQLException

      Retrieve the given column as character stream from the given ResultSet. Might simply invoke `ResultSet.getCharacterStream` or work with `ResultSet.getClob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnName` - the column name to use
      Returns:
        the content as character stream
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getCharacterStream(int)`

    - #### getClobAsCharacterStream

          Reader getClobAsCharacterStream(ResultSet rs,
                                        int columnIndex)
                                          throws SQLException

      Retrieve the given column as character stream from the given ResultSet. Might simply invoke `ResultSet.getCharacterStream` or work with `ResultSet.getClob`, depending on the database and driver.
      Parameters:
        `rs` - the ResultSet to retrieve the content from
        `columnIndex` - the column index to use
      Returns:
        the content as character stream
      Throws:
        `SQLException` - if thrown by JDBC methods
      See Also:
        `ResultSet.getCharacterStream(int)`

    - #### getLobCreator

          LobCreator getLobCreator()

      Create a new `LobCreator` instance, i.e. a session for creating BLOBs and CLOBs. Needs to be closed after the created LOBs are not needed anymore - typically after statement execution or transaction completion.
      Returns:
        the new LobCreator instance
      See Also:
        `LobCreator.close()`