01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.core.support;
18:
19: import java.sql.PreparedStatement;
20: import java.sql.SQLException;
21:
22: import org.springframework.dao.DataAccessException;
23: import org.springframework.jdbc.core.PreparedStatementCallback;
24: import org.springframework.jdbc.support.lob.LobCreator;
25: import org.springframework.jdbc.support.lob.LobHandler;
26:
27: /**
28: * Abstract PreparedStatementCallback implementation that manages a LobCreator.
29: * Typically used as inner class, with access to surrounding method arguments.
30: *
31: * <p>Delegates to the <code>setValues</code> template method for setting values
32: * on the PreparedStatement, using a given LobCreator for BLOB/CLOB arguments.
33: *
34: * <p>A usage example with JdbcTemplate:
35: *
36: * <pre class="code">JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object
37: * LobHandler lobHandler = new DefaultLobHandler(); // reusable object
38: *
39: * jdbcTemplate.execute(
40: * "INSERT INTO imagedb (image_name, content, description) VALUES (?, ?, ?)",
41: * new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
42: * protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
43: * ps.setString(1, name);
44: * lobCreator.setBlobAsBinaryStream(ps, 2, contentStream, contentLength);
45: * lobCreator.setClobAsString(ps, 3, description);
46: * }
47: * }
48: * );</pre>
49: *
50: * @author Juergen Hoeller
51: * @since 1.0.2
52: * @see org.springframework.jdbc.support.lob.LobCreator
53: */
54: public abstract class AbstractLobCreatingPreparedStatementCallback
55: implements PreparedStatementCallback {
56:
57: private final LobHandler lobHandler;
58:
59: /**
60: * Create a new AbstractLobCreatingPreparedStatementCallback for the
61: * given LobHandler.
62: * @param lobHandler the LobHandler to create LobCreators with
63: */
64: public AbstractLobCreatingPreparedStatementCallback(
65: LobHandler lobHandler) {
66: this .lobHandler = lobHandler;
67: }
68:
69: public final Object doInPreparedStatement(PreparedStatement ps)
70: throws SQLException, DataAccessException {
71: LobCreator lobCreator = this .lobHandler.getLobCreator();
72: try {
73: setValues(ps, lobCreator);
74: return new Integer(ps.executeUpdate());
75: } finally {
76: lobCreator.close();
77: }
78: }
79:
80: /**
81: * Set values on the given PreparedStatement, using the given
82: * LobCreator for BLOB/CLOB arguments.
83: * @param ps the PreparedStatement to use
84: * @param lobCreator the LobCreator to use
85: * @throws SQLException if thrown by JDBC methods
86: * @throws DataAccessException in case of custom exceptions
87: */
88: protected abstract void setValues(PreparedStatement ps,
89: LobCreator lobCreator) throws SQLException,
90: DataAccessException;
91:
92: }
|