KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > lob > DefaultLobHandler


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.support.lob;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /**
29  * Default implementation of the LobHandler interface. Invokes the
30  * direct accessor methods that <code>java.sql.ResultSet</code> and
31  * <code>java.sql.PreparedStatement</code> offer.
32  *
33  * <p>This LobHandler should work for any JDBC driver that is JDBC compliant
34  * in terms of the spec's suggestions regarding simple BLOB and CLOB handling.
35  * This does not apply to Oracle 9i, and only to a limited degree to Oracle 10g!
36  * As a consequence, use OracleLobHandler for accessing Oracle BLOBs/CLOBs.
37  *
38  * @author Juergen Hoeller
39  * @since 04.12.2003
40  * @see OracleLobHandler
41  * @see java.sql.ResultSet#getBytes
42  * @see java.sql.ResultSet#getBinaryStream
43  * @see java.sql.ResultSet#getString
44  * @see java.sql.ResultSet#getAsciiStream
45  * @see java.sql.ResultSet#getCharacterStream
46  * @see java.sql.PreparedStatement#setBytes
47  * @see java.sql.PreparedStatement#setBinaryStream
48  * @see java.sql.PreparedStatement#setString
49  * @see java.sql.PreparedStatement#setAsciiStream
50  * @see java.sql.PreparedStatement#setCharacterStream
51  */

52 public class DefaultLobHandler extends AbstractLobHandler {
53
54     protected final Log logger = LogFactory.getLog(getClass());
55
56
57     public byte[] getBlobAsBytes(ResultSet JavaDoc rs, int columnIndex) throws SQLException JavaDoc {
58         logger.debug("Returning BLOB as bytes");
59         return rs.getBytes(columnIndex);
60     }
61
62     public InputStream JavaDoc getBlobAsBinaryStream(ResultSet JavaDoc rs, int columnIndex) throws SQLException JavaDoc {
63         logger.debug("Returning BLOB as binary stream");
64         return rs.getBinaryStream(columnIndex);
65     }
66
67     public String JavaDoc getClobAsString(ResultSet JavaDoc rs, int columnIndex) throws SQLException JavaDoc {
68         logger.debug("Returning CLOB as string");
69         return rs.getString(columnIndex);
70     }
71
72     public InputStream JavaDoc getClobAsAsciiStream(ResultSet JavaDoc rs, int columnIndex) throws SQLException JavaDoc {
73         logger.debug("Returning CLOB as ASCII stream");
74         return rs.getAsciiStream(columnIndex);
75     }
76
77     public Reader JavaDoc getClobAsCharacterStream(ResultSet JavaDoc rs, int columnIndex) throws SQLException JavaDoc {
78         logger.debug("Returning CLOB as character stream");
79         return rs.getCharacterStream(columnIndex);
80     }
81
82     public LobCreator getLobCreator() {
83         logger.debug("Creating new DefaultLobCreator");
84         return new DefaultLobCreator();
85     }
86
87
88     protected class DefaultLobCreator implements LobCreator {
89
90         public void setBlobAsBytes(PreparedStatement JavaDoc ps, int paramIndex, byte[] content)
91                 throws SQLException JavaDoc {
92
93             ps.setBytes(paramIndex, content);
94             if (logger.isDebugEnabled()) {
95                 logger.debug(content != null ? "Set bytes for BLOB with length " + content.length :
96                         "Set BLOB to null");
97             }
98         }
99
100         public void setBlobAsBinaryStream(
101                 PreparedStatement JavaDoc ps, int paramIndex, InputStream JavaDoc binaryStream, int contentLength)
102                 throws SQLException JavaDoc {
103
104             ps.setBinaryStream(paramIndex, binaryStream, contentLength);
105             if (logger.isDebugEnabled()) {
106                 logger.debug(binaryStream != null ? "Set binary stream for BLOB with length " + contentLength :
107                         "Set BLOB to null");
108             }
109         }
110
111         public void setClobAsString(PreparedStatement JavaDoc ps, int paramIndex, String JavaDoc content)
112             throws SQLException JavaDoc {
113
114             ps.setString(paramIndex, content);
115             if (logger.isDebugEnabled()) {
116                 logger.debug(content != null ? "Set string for CLOB with length " + content.length() :
117                         "Set CLOB to null");
118             }
119         }
120
121         public void setClobAsAsciiStream(
122                 PreparedStatement JavaDoc ps, int paramIndex, InputStream JavaDoc asciiStream, int contentLength)
123             throws SQLException JavaDoc {
124
125             ps.setAsciiStream(paramIndex, asciiStream, contentLength);
126             if (logger.isDebugEnabled()) {
127                 logger.debug(asciiStream != null ? "Set ASCII stream for CLOB with length " + contentLength :
128                         "Set CLOB to null");
129             }
130         }
131
132
133         public void setClobAsCharacterStream(
134                 PreparedStatement JavaDoc ps, int paramIndex, Reader JavaDoc characterStream, int contentLength)
135             throws SQLException JavaDoc {
136
137             ps.setCharacterStream(paramIndex, characterStream, contentLength);
138             if (logger.isDebugEnabled()) {
139                 logger.debug(characterStream != null ? "Set character stream for CLOB with length " + contentLength :
140                         "Set CLOB to null");
141             }
142         }
143
144         public void close() {
145             logger.debug("Closing DefaultLobCreator");
146             // nothing to do here
147
}
148     }
149
150 }
151
Popular Tags