Top Description Methods
org.springframework.jdbc.support.lob

public Interface LobHandler

Known Direct Implementers
org.springframework.jdbc.support.lob.AbstractLobHandler
Imports
java.io.InputStream, .Reader, java.sql.ResultSet, .SQLException, org.springframework.lang.Nullable

Abstraction for handling large binary fields and large text fields in specific databases, no matter if represented as simple types or Large OBjects.

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.

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):

Author
Juergen Hoeller
Since
23.12.2003
See Also
DefaultLobHandler, java.sql.ResultSet#getBlob, java.sql.ResultSet#getClob, java.sql.ResultSet#getBytes, java.sql.ResultSet#getBinaryStream, java.sql.ResultSet#getString, java.sql.ResultSet#getAsciiStream, java.sql.ResultSet#getCharacterStream

Method Summary

Modifier and TypeMethod and Description
public InputStream

Returns:

the content as binary stream, or null in case of SQL NULL
getBlobAsBinaryStream
(ResultSet
the ResultSet to retrieve the content from
rs
,
String
the column name to use
columnName
)

Retrieve the given column as binary stream from the given ResultSet.

public InputStream

Returns:

the content as binary stream, or null in case of SQL NULL
getBlobAsBinaryStream
(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Retrieve the given column as binary stream from the given ResultSet.

public byte[]

Returns:

the content as byte array, or null in case of SQL NULL
getBlobAsBytes
(ResultSet
the ResultSet to retrieve the content from
rs
,
String
the column name to use
columnName
)

Retrieve the given column as bytes from the given ResultSet.

public byte[]

Returns:

the content as byte array, or null in case of SQL NULL
getBlobAsBytes
(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Retrieve the given column as bytes from the given ResultSet.

public InputStream

Returns:

the content as ASCII stream, or null in case of SQL NULL
getClobAsAsciiStream
(ResultSet
the ResultSet to retrieve the content from
rs
,
String
the column name to use
columnName
)

Retrieve the given column as ASCII stream from the given ResultSet.

public InputStream

Returns:

the content as ASCII stream, or null in case of SQL NULL
getClobAsAsciiStream
(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Retrieve the given column as ASCII stream from the given ResultSet.

public Reader

Returns:

the content as character stream
getClobAsCharacterStream
(ResultSet
the ResultSet to retrieve the content from
rs
,
String
the column name to use
columnName
)

Retrieve the given column as character stream from the given ResultSet.

public Reader

Returns:

the content as character stream
getClobAsCharacterStream
(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Retrieve the given column as character stream from the given ResultSet.

public String

Returns:

the content as String, or null in case of SQL NULL
getClobAsString
(ResultSet
the ResultSet to retrieve the content from
rs
,
String
the column name to use
columnName
)

Retrieve the given column as String from the given ResultSet.

public String

Returns:

the content as String, or null in case of SQL NULL
getClobAsString
(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Retrieve the given column as String from the given ResultSet.

public LobCreator

Returns:

the new LobCreator instance
getLobCreator
()

Create a new LobCreator instance, i.e. a session for creating BLOBs and CLOBs.

Method Detail

getBlobAsBinaryStreamback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnName:String

the column name to use

Returns:InputStream

the content as binary stream, or null in case of SQL NULL

Annotations
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getBinaryStream
getBlobAsBinaryStreamback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:InputStream

the content as binary stream, or null in case of SQL NULL

Annotations
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getBinaryStream
getBlobAsBytesback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnName:String

the column name to use

Returns:byte[]

the content as byte array, or null in case of SQL NULL

Annotations
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getBytes
getBlobAsBytesback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:byte[]

the content as byte array, or null in case of SQL NULL

Annotations
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getBytes
getClobAsAsciiStreamback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnName:String

the column name to use

Returns:InputStream

the content as ASCII stream, or null in case of SQL NULL

Annotations
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getAsciiStream
getClobAsAsciiStreamback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:InputStream

the content as ASCII stream, or null in case of SQL NULL

Annotations
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getAsciiStream
getClobAsCharacterStreamback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnName:String

the column name to use

Returns:Reader

the content as character stream

Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getCharacterStream
getClobAsCharacterStreamback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:Reader

the content as character stream

Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getCharacterStream
getClobAsStringback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnName:String

the column name to use

Returns:String

the content as String, or null in case of SQL NULL

Annotations
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getString
getClobAsStringback to summary
public 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:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:String

the content as String, or null in case of SQL NULL

Annotations
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
See Also
java.sql.ResultSet#getString
getLobCreatorback to summary
public 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:LobCreator

the new LobCreator instance

See Also
LobCreator#close()