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