@Test(expected = RuntimeException.class) public void testGetById() throws SQLException { UserDAO dao = new SQLUserDAO(connection); IUser user = dao.getById(42); assertEquals("Micky", user.getFirstName()); assertEquals("Mouse", user.getName()); assertEquals(42, user.getId()); connection.close(); dao.getById(42); }
@Override public IUser getById(int id) { try { User result = null; PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, id); ResultSet r = ps.executeQuery(); if (r.next()) { result = readUser(r); } r.close(); ps.close(); return result; } catch (SQLException e) { throw new RuntimeException(e); } }
SQLUserDAO implements UserDAO { @Override public IUser getById(int id) { try { User result = null; PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, id); ResultSet r = ps.executeQuery(); if (r.next()) { result = readUser(r); } r.close(); ps.close(); return result; } catch (SQLException e) { throw new RuntimeException(e); } } }
SQLUserDAO implements UserDAO { @Override public IUser getById(int id) { try { User result = null; PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, id); ResultSet r = ps.executeQuery(); if (r.next()) { result = readUser(r); } r.close(); ps.close(); return result; } catch (SQLException e) { throw new RuntimeException(e); } } SQLUserDAO(Connection c); }
SQLUserDAO implements UserDAO { @Override public IUser getById(int id) { try { User result = null; PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, id); ResultSet r = ps.executeQuery(); if (r.next()) { result = readUser(r); } r.close(); ps.close(); return result; } catch (SQLException e) { throw new RuntimeException(e); } } SQLUserDAO(Connection c); @Override void delete(IUser user); @Override List<IUser> getAll(); @Override IUser getById(int id); @Override List<IUser> getByName(String name); @Override void saveOrUpdate(IUser user); }
SQLUserDAO implements UserDAO { @Override public IUser getById(int id) { try { User result = null; PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, id); ResultSet r = ps.executeQuery(); if (r.next()) { result = readUser(r); } r.close(); ps.close(); return result; } catch (SQLException e) { throw new RuntimeException(e); } } SQLUserDAO(Connection c); @Override void delete(IUser user); @Override List<IUser> getAll(); @Override IUser getById(int id); @Override List<IUser> getByName(String name); @Override void saveOrUpdate(IUser user);  }