/db-diff/src/main/java/com/googlecode/hdbc/dbdiff/db/QueryExecutionTemplate.java

http://hdbc.googlecode.com/ · Java · 39 lines · 35 code · 4 blank · 0 comment · 4 complexity · 0acd8d3c4d7b8993df0f532be4632dcf MD5 · raw file

  1. package com.googlecode.hdbc.dbdiff.db;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import javax.sql.DataSource;
  6. import com.googlecode.hdbc.dbdiff.db.IQueryDefinition;
  7. import com.googlecode.hdbc.dbdiff.model.DbObjects;
  8. public class QueryExecutionTemplate<T> {
  9. public final DbObjects<T> executeQuery(final IQueryDefinition<T> definition,
  10. final DataSource ods) throws SQLException {
  11. Connection cnn = null;
  12. PreparedStatement stmt = null;
  13. try {
  14. cnn = ods.getConnection();
  15. stmt = cnn.prepareStatement(definition.getParameterizedSQL());
  16. definition.processStatement(stmt);
  17. stmt.close();
  18. stmt = null;
  19. } catch (SQLException e) {
  20. throw new IllegalStateException(e);
  21. } finally {
  22. if (stmt != null) {
  23. try {
  24. stmt.close();
  25. } catch (SQLException e) { }
  26. }
  27. if (cnn != null) {
  28. try {
  29. cnn.close();
  30. } catch (SQLException e) { }
  31. }
  32. }
  33. return definition.result();
  34. }
  35. }