1 16 package scriptella.jdbc; 17 18 import scriptella.spi.MockParametersCallbacks; 19 import scriptella.spi.QueryCallback; 20 import scriptella.util.IOUtils; 21 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 import java.sql.Statement ; 25 26 32 public class QueryHelper { 33 private String sql; 34 35 public QueryHelper(String sql) { 36 this.sql = sql; 37 } 38 39 44 public void execute(Connection con, QueryCallback callback) { 45 Statement st = null; 46 ResultSetAdapter ra = null; 47 try { 48 st=con.createStatement(); 49 ra = new ResultSetAdapter(st.executeQuery(sql), 50 MockParametersCallbacks.UNSUPPORTED, new JdbcTypesConverter()); 51 while (ra.next()) { 52 callback.processRow(ra); 53 } 54 } catch (SQLException e) { 55 throw new IllegalStateException (e.getMessage(), e); 56 } finally { 57 IOUtils.closeSilently(ra); 58 JdbcUtils.closeSilent(st); 59 } 60 61 } 62 63 protected void onSQLException(SQLException e) { 64 throw new IllegalStateException (e.getMessage(), e); 65 } 66 } 67 | Popular Tags |