---
title: "org.springframework.jdbc.support.lob.DefaultLobHandler"
author: "hai0378"
date: "2013-05-09T12:47:05Z"
tags: []
categories: ["开源软件"]
slug: "org-springframework-jdbc-support-lob-defaultlobhandler"
draft: false
lastmod: "2013-05-09T12:47:05Z"
id: 1864976
url: "https://hai0378.iteye.com/blog/1864976"
---

[]()

&lt;!-- ========= END OF TOP NAVBAR ========= --&gt;

 链接如下：

[http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/DefaultLobHandler.html](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/DefaultLobHandler.html)

* * *

&lt;!-- ======== START OF CLASS DATA ======== --&gt;

## org.springframework.jdbc.support.lob Class DefaultLobHandler

```
java.lang.Object
  
```

![extended by ](http://static.springsource.org/spring/docs/2.5.x/api/resources/inherit.gif)

```
org.springframework.jdbc.support.lob.AbstractLobHandler
      
```

![extended by ](http://static.springsource.org/spring/docs/2.5.x/api/resources/inherit.gif)

```
org.springframework.jdbc.support.lob.DefaultLobHandler
```

**All Implemented Interfaces:**

[LobHandler](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/LobHandler.html "interface in org.springframework.jdbc.support.lob")

* * *

 

```
public class DefaultLobHandlerextends AbstractLobHandler
```

```

```

Default implementation of the [`LobHandler`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/LobHandler.html "interface in org.springframework.jdbc.support.lob") interface. Invokes the direct accessor methods that `java.sql.ResultSet` and `java.sql.PreparedStatement` offer.

This LobHandler should work for any JDBC driver that is JDBC compliant in terms of the spec's suggestions regarding simple BLOB and CLOB handling. This does not apply to Oracle 9i, and only to a limited degree to Oracle 10g! As a consequence, use [`OracleLobHandler`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/OracleLobHandler.html "class in org.springframework.jdbc.support.lob") for accessing Oracle BLOBs/CLOBs.

Some JDBC drivers require values with a BLOB/CLOB target column to be explicitly set through the JDBC `setBlob` / `setClob` API: for example, PostgreSQL's driver. Switch the [`"wrapAsLob"`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/DefaultLobHandler.html#setWrapAsLob%28boolean%29) property to "true" when operating against such a driver.

On JDBC 4.0, this LobHandler also supports streaming the BLOB/CLOB content via the `setBlob` / `setClob` variants that take a stream argument directly. Consider switching the [`"streamAsLob"`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/DefaultLobHandler.html#setStreamAsLob%28boolean%29) property to "true" when operating against a fully compliant JDBC 4.0 driver.

See the [`LobHandler`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/LobHandler.html "interface in org.springframework.jdbc.support.lob") javadoc for a summary of recommendations.

**Since:**

04.12.2003

**Author:**

Juergen Hoeller

**See Also:**

[`setStreamAsLob(boolean)`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/DefaultLobHandler.html#setStreamAsLob%28boolean%29), [`ResultSet.getBytes(int)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getBytes%28int%29 "class or interface in java.sql"), [`ResultSet.getBinaryStream(int)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getBinaryStream%28int%29 "class or interface in java.sql"), [`ResultSet.getString(int)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getString%28int%29 "class or interface in java.sql"), [`ResultSet.getAsciiStream(int)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getAsciiStream%28int%29 "class or interface in java.sql"), [`ResultSet.getCharacterStream(int)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getCharacterStream%28int%29 "class or interface in java.sql"), [`PreparedStatement.setBytes(int, byte[])`](http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html?is-external=true#setBytes%28int,%20byte%5B%5D%29 "class or interface in java.sql"), [`PreparedStatement.setBinaryStream(int, java.io.InputStream, int)`](http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html?is-external=true#setBinaryStream%28int,%20java.io.InputStream,%20int%29 "class or interface in java.sql"), [`PreparedStatement.setString(int, java.lang.String)`](http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html?is-external=true#setString%28int,%20java.lang.String%29 "class or interface in java.sql"), [`PreparedStatement.setAsciiStream(int, java.io.InputStream, int)`](http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html?is-external=true#setAsciiStream%28int,%20java.io.InputStream,%20int%29 "class or interface in java.sql"), [`PreparedStatement.setCharacterStream(int, java.io.Reader, int)`](http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html?is-external=true#setCharacterStream%28int,%20java.io.Reader,%20int%29 "class or interface in java.sql")

* * *

 

&lt;!-- ======== NESTED CLASS SUMMARY ======== --&gt;[]()&lt;!-- --&gt;

**Nested Class Summary** `protected  class` `DefaultLobHandler.DefaultLobCreator`  
          Default LobCreator implementation as inner class.

 

&lt;!-- =========== FIELD SUMMARY =========== --&gt;

[]()&lt;!-- --&gt;

**Field Summary** `protected  Log` `logger`  
           

 

&lt;!-- ======== CONSTRUCTOR SUMMARY ======== --&gt;

[]()&lt;!-- --&gt;

**Constructor Summary** `DefaultLobHandler()`  
           

 

&lt;!-- ========== METHOD SUMMARY =========== --&gt;

[]()&lt;!-- --&gt;

**Method Summary** `InputStream` `getBlobAsBinaryStream(ResultSet rs, int columnIndex)`  
          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. `InputStream` `getClobAsAsciiStream(ResultSet rs, int columnIndex)`  
          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. `String` `getClobAsString(ResultSet rs, int columnIndex)`  
          Retrieve the given column as String from the given ResultSet. `LobCreator` `getLobCreator()`  
          Create a new [`LobCreator`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/LobCreator.html "interface in org.springframework.jdbc.support.lob") instance, i.e. a session for creating BLOBs and CLOBs. `void` `setStreamAsLob(boolean streamAsLob)`  
          Specify whether to submit a binary stream / character stream to the JDBC driver as explicit LOB content, using the JDBC 4.0 `setBlob` / `setClob` method with a stream argument. `void` `setWrapAsLob(boolean wrapAsLob)`  
          Specify whether to submit a byte array / String to the JDBC driver wrapped in a JDBC Blob / Clob object, using the JDBC `setBlob` / `setClob` method with a Blob / Clob argument.

 []()&lt;!-- --&gt;

**Methods inherited from class org.springframework.jdbc.support.lob.[AbstractLobHandler](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/AbstractLobHandler.html "class in org.springframework.jdbc.support.lob")** `getBlobAsBinaryStream, getBlobAsBytes, getClobAsAsciiStream, getClobAsCharacterStream, getClobAsString`

 []()&lt;!-- --&gt;

**Methods inherited from class java.lang.[Object](http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true "class or interface in java.lang")** `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

 

&lt;!-- ============ FIELD DETAIL =========== --&gt;[]()&lt;!-- --&gt;

**Field Detail**

[]()&lt;!-- --&gt;

### logger

```
protected final Log logger
```

&lt;!-- ========= CONSTRUCTOR DETAIL ======== --&gt;

[]()&lt;!-- --&gt;

**Constructor Detail**

[]()&lt;!-- --&gt;

### DefaultLobHandler

```
public DefaultLobHandler()
```

&lt;!-- ============ METHOD DETAIL ========== --&gt;

[]()&lt;!-- --&gt;

**Method Detail**

[]()&lt;!-- --&gt;

### setWrapAsLob

```
public void setWrapAsLob(boolean wrapAsLob)
```

Specify whether to submit a byte array / String to the JDBC driver wrapped in a JDBC Blob / Clob object, using the JDBC `setBlob` / `setClob` method with a Blob / Clob argument.

Default is "false", using the common JDBC 2.0 `setBinaryStream` / `setCharacterStream` method for setting the content. Switch this to "true" for explicit Blob / Clob wrapping against JDBC drivers that are known to require such wrapping (e.g. PostgreSQL's).

This setting affects byte array / String arguments as well as stream arguments, unless [`"streamAsLob"`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/DefaultLobHandler.html#setStreamAsLob%28boolean%29) overrides this handling to use JDBC 4.0's new explicit streaming support (if available).

 

**See Also:**

[`PreparedStatement.setBlob(int, java.sql.Blob)`](http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html?is-external=true#setBlob%28int,%20java.sql.Blob%29 "class or interface in java.sql"), [`PreparedStatement.setClob(int, java.sql.Clob)`](http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html?is-external=true#setClob%28int,%20java.sql.Clob%29 "class or interface in java.sql")

* * *

[]()&lt;!-- --&gt;

 

### setStreamAsLob

```
public void setStreamAsLob(boolean streamAsLob)
```

Specify whether to submit a binary stream / character stream to the JDBC driver as explicit LOB content, using the JDBC 4.0 `setBlob` / `setClob` method with a stream argument.

Default is "false", using the common JDBC 2.0 `setBinaryStream` / `setCharacterStream` method for setting the content. Switch this to "true" for explicit JDBC 4.0 usage, provided that your JDBC driver actually supports those JDBC 4.0 operations (e.g. Derby's).

This setting affects stream arguments as well as byte array / String arguments, requiring JDBC 4.0 support. For supporting LOB content against JDBC 3.0, check out the [`"wrapAsLob"`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/DefaultLobHandler.html#setWrapAsLob%28boolean%29) setting.

 

**See Also:**

[`PreparedStatement.setBlob(int, java.io.InputStream, long)`](http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html?is-external=true#setBlob%28int,%20java.io.InputStream,%20long%29 "class or interface in java.sql"), [`PreparedStatement.setClob(int, java.io.Reader, long)`](http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html?is-external=true#setClob%28int,%20java.io.Reader,%20long%29 "class or interface in java.sql")

* * *

[]()&lt;!-- --&gt;

 

### getBlobAsBytes

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

**Description copied from interface: `LobHandler`**

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)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getBytes%28int%29 "class or interface in java.sql")

* * *

[]()&lt;!-- --&gt;

 

### getBlobAsBinaryStream

```
public InputStream getBlobAsBinaryStream(ResultSet rs,
                                         int columnIndex)
                                  throws SQLException
```

**Description copied from interface: `LobHandler`**

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)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getBinaryStream%28int%29 "class or interface in java.sql")

* * *

[]()&lt;!-- --&gt;

 

### getClobAsString

```
public String getClobAsString(ResultSet rs,
                              int columnIndex)
                       throws SQLException
```

**Description copied from interface: `LobHandler`**

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)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getString%28int%29 "class or interface in java.sql")

* * *

[]()&lt;!-- --&gt;

 

### getClobAsAsciiStream

```
public InputStream getClobAsAsciiStream(ResultSet rs,
                                        int columnIndex)
                                 throws SQLException
```

**Description copied from interface: `LobHandler`**

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)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getAsciiStream%28int%29 "class or interface in java.sql")

* * *

[]()&lt;!-- --&gt;

 

### getClobAsCharacterStream

```
public Reader getClobAsCharacterStream(ResultSet rs,
                                       int columnIndex)
                                throws SQLException
```

**Description copied from interface: `LobHandler`**

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)`](http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html?is-external=true#getCharacterStream%28int%29 "class or interface in java.sql")

* * *

[]()&lt;!-- --&gt;

 

### getLobCreator

```
public LobCreator getLobCreator()
```

**Description copied from interface: `LobHandler`**

Create a new [`LobCreator`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/LobCreator.html "interface in org.springframework.jdbc.support.lob") 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()`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/lob/LobCreator.html#close%28%29)