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;
  17. import java.sql.PreparedStatement;
  18. import java.sql.SQLException;
  19. import org.springframework.dao.DataAccessException;
  20. /**
  21. * Generic callback interface for code that operates on a PreparedStatement.
  22. * Allows to execute any number of operations on a single PreparedStatement,
  23. * for example a single executeUpdate call or repeated executeUpdate calls
  24. * with varying parameters.
  25. *
  26. * <p>Used internally by JdbcTemplate, but also useful for application code.
  27. * Note that the passed-in PreparedStatement can have been created by the
  28. * framework or by a custom PreparedStatementCreator. However, the latter is
  29. * hardly ever necessary, as most custom callback actions will perform updates
  30. * in which case a standard PreparedStatement is fine. Custom actions will
  31. * always set parameter values themselves, so that PreparedStatementCreator
  32. * capability is not needed either.
  33. *
  34. * @author Juergen Hoeller
  35. * @since 16.03.2004
  36. * @see JdbcTemplate#execute(String, PreparedStatementCallback)
  37. * @see JdbcTemplate#execute(PreparedStatementCreator, PreparedStatementCallback)
  38. */
  39. public interface PreparedStatementCallback {
  40. /**
  41. * Gets called by JdbcTemplate.execute with an active JDBC PreparedStatement.
  42. * Does not need to care about activating or closing the Connection,
  43. * or handling transactions.
  44. *
  45. * <p>If called without a thread-bound JDBC transaction (initiated by
  46. * DataSourceTransactionManager), the code will simply get executed on the
  47. * JDBC connection with its transactional semantics. If JdbcTemplate is
  48. * configured to use a JTA-aware DataSource, the JDBC connection and thus
  49. * the callback code will be transactional if a JTA transaction is active.
  50. *
  51. * <p>Allows for returning a result object created within the callback, i.e.
  52. * a domain object or a collection of domain objects. Note that there's
  53. * special support for single step actions: see JdbcTemplate.queryForObject etc.
  54. * A thrown RuntimeException is treated as application exception, it gets
  55. * propagated to the caller of the template.
  56. *
  57. * @param ps active JDBC PreparedStatement
  58. * @return a result object, or null if none
  59. * @throws SQLException if thrown by a JDBC method, to be auto-converted
  60. * into a DataAccessException by a SQLExceptionTranslator
  61. * @throws DataAccessException in case of custom exceptions
  62. * @see JdbcTemplate#queryForObject(String, Object[], Class)
  63. * @see JdbcTemplate#queryForList(String, Object[])
  64. */
  65. Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException;
  66. }