KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > support > AbstractLobCreatingPreparedStatementCallback


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.core.support;
18
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.SQLException JavaDoc;
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 implements PreparedStatementCallback {
55
56     private final LobHandler lobHandler;
57
58     /**
59      * Create a new AbstractLobCreatingPreparedStatementCallback for the
60      * given LobHandler.
61      * @param lobHandler the LobHandler to create LobCreators with
62      */

63     public AbstractLobCreatingPreparedStatementCallback(LobHandler lobHandler) {
64         this.lobHandler = lobHandler;
65     }
66
67     public final Object JavaDoc doInPreparedStatement(PreparedStatement JavaDoc ps) throws SQLException JavaDoc, DataAccessException {
68         LobCreator lobCreator = this.lobHandler.getLobCreator();
69         try {
70             setValues(ps, lobCreator);
71             return new Integer JavaDoc(ps.executeUpdate());
72         }
73         finally {
74             lobCreator.close();
75         }
76     }
77
78     /**
79      * Set values on the given PreparedStatement, using the given
80      * LobCreator for BLOB/CLOB arguments.
81      * @param ps the PreparedStatement to use
82      * @param lobCreator the LobCreator to use
83      * @throws SQLException if thrown by JDBC methods
84      * @throws DataAccessException in case of custom exceptions
85      */

86     protected abstract void setValues(PreparedStatement JavaDoc ps, LobCreator lobCreator)
87             throws SQLException JavaDoc, DataAccessException;
88
89 }
90
Popular Tags