AbstractLobCreatingPreparedStatementCallback.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

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