public interface LobHandler
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):
DefaultLobHandler
,
potentially with streamAsLob=true
if your database driver requires that
hint when populating a LOB field. Fall back to createTemporaryLob=true
if you happen to run into LOB size limitations with your (Oracle) database setup.
DefaultLobHandler
with standard setup.
On Oracle 10.1, set the "SetBigStringTryClob" connection property; as of Oracle 10.2,
DefaultLobHandler should work with standard setup out of the box.
DefaultLobHandler
with wrapAsLob=true
,
and use that LobHandler to access OID columns (but not BYTEA) in your database tables.
DefaultLobHandler
.
DefaultLobHandler
,
ResultSet.getBlob(int)
,
ResultSet.getClob(int)
,
ResultSet.getBytes(int)
,
ResultSet.getBinaryStream(int)
,
ResultSet.getString(int)
,
ResultSet.getAsciiStream(int)
,
ResultSet.getCharacterStream(int)
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. |
@Nullable byte[] getBlobAsBytes(ResultSet rs, String columnName) throws SQLException
ResultSet.getBytes
or work with
ResultSet.getBlob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnName
- the column name to usenull
in case of SQL NULLSQLException
- if thrown by JDBC methodsResultSet.getBytes(int)
@Nullable byte[] getBlobAsBytes(ResultSet rs, int columnIndex) throws SQLException
ResultSet.getBytes
or work with
ResultSet.getBlob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnIndex
- the column index to usenull
in case of SQL NULLSQLException
- if thrown by JDBC methodsResultSet.getBytes(int)
@Nullable InputStream getBlobAsBinaryStream(ResultSet rs, String columnName) throws SQLException
ResultSet.getBinaryStream
or work with
ResultSet.getBlob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnName
- the column name to usenull
in case of SQL NULLSQLException
- if thrown by JDBC methodsResultSet.getBinaryStream(int)
@Nullable InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException
ResultSet.getBinaryStream
or work with
ResultSet.getBlob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnIndex
- the column index to usenull
in case of SQL NULLSQLException
- if thrown by JDBC methodsResultSet.getBinaryStream(int)
@Nullable String getClobAsString(ResultSet rs, String columnName) throws SQLException
ResultSet.getString
or work with
ResultSet.getClob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnName
- the column name to usenull
in case of SQL NULLSQLException
- if thrown by JDBC methodsResultSet.getString(int)
@Nullable String getClobAsString(ResultSet rs, int columnIndex) throws SQLException
ResultSet.getString
or work with
ResultSet.getClob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnIndex
- the column index to usenull
in case of SQL NULLSQLException
- if thrown by JDBC methodsResultSet.getString(int)
@Nullable InputStream getClobAsAsciiStream(ResultSet rs, String columnName) throws SQLException
ResultSet.getAsciiStream
or work with
ResultSet.getClob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnName
- the column name to usenull
in case of SQL NULLSQLException
- if thrown by JDBC methodsResultSet.getAsciiStream(int)
@Nullable InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) throws SQLException
ResultSet.getAsciiStream
or work with
ResultSet.getClob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnIndex
- the column index to usenull
in case of SQL NULLSQLException
- if thrown by JDBC methodsResultSet.getAsciiStream(int)
Reader getClobAsCharacterStream(ResultSet rs, String columnName) throws SQLException
ResultSet.getCharacterStream
or work with
ResultSet.getClob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnName
- the column name to useSQLException
- if thrown by JDBC methodsResultSet.getCharacterStream(int)
Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException
ResultSet.getCharacterStream
or work with
ResultSet.getClob
, depending on the database and driver.rs
- the ResultSet to retrieve the content fromcolumnIndex
- the column index to useSQLException
- if thrown by JDBC methodsResultSet.getCharacterStream(int)
LobCreator getLobCreator()
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.LobCreator.close()