@Test public void shouldSelectOne() throws Exception { DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES); runScript(ds, JPETSTORE_DDL); runScript(ds, JPETSTORE_DATA); try (Connection connection = ds.getConnection()) { SqlRunner exec = new SqlRunner(connection); Map<String, Object> row = exec.selectOne("SELECT * FROM PRODUCT WHERE PRODUCTID = ?", "FI-SW-01"); assertEquals("FI-SW-01", row.get("PRODUCTID")); } }
public Map<String, Object> selectOne(String sql, Object... args) throws SQLException { List<Map<String, Object>> results = selectAll(sql, args); if (results.size() != 1) { throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected."); } return results.get(0); }
SqlRunner { public Map<String, Object> selectOne(String sql, Object... args) throws SQLException { List<Map<String, Object>> results = selectAll(sql, args); if (results.size() != 1) { throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected."); } return results.get(0); } }
SqlRunner { public Map<String, Object> selectOne(String sql, Object... args) throws SQLException { List<Map<String, Object>> results = selectAll(sql, args); if (results.size() != 1) { throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected."); } return results.get(0); } SqlRunner(Connection connection); }
SqlRunner { public Map<String, Object> selectOne(String sql, Object... args) throws SQLException { List<Map<String, Object>> results = selectAll(sql, args); if (results.size() != 1) { throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected."); } return results.get(0); } SqlRunner(Connection connection); void setUseGeneratedKeySupport(boolean useGeneratedKeySupport); Map<String, Object> selectOne(String sql, Object... args); List<Map<String, Object>> selectAll(String sql, Object... args); int insert(String sql, Object... args); int update(String sql, Object... args); int delete(String sql, Object... args); void run(String sql); void closeConnection(); }
SqlRunner { public Map<String, Object> selectOne(String sql, Object... args) throws SQLException { List<Map<String, Object>> results = selectAll(sql, args); if (results.size() != 1) { throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected."); } return results.get(0); } SqlRunner(Connection connection); void setUseGeneratedKeySupport(boolean useGeneratedKeySupport); Map<String, Object> selectOne(String sql, Object... args); List<Map<String, Object>> selectAll(String sql, Object... args); int insert(String sql, Object... args); int update(String sql, Object... args); int delete(String sql, Object... args); void run(String sql); void closeConnection(); static final int NO_GENERATED_KEY; }