@Test(expected = RuntimeException.class) public void testSaveOrUpdate() throws SQLException { Statement s = connection.createStatement(); ResultSet r = s.executeQuery(COUNT_SQL); r.next(); int rows0 = r.getInt(1); UserDAO dao = new SQLUserDAO(connection); IUser daisy = dao.getById(13); daisy.setFirstName("Daisy"); dao.saveOrUpdate(daisy); IUser actual = dao.getById(13); assertEquals(daisy.getFirstName(), actual.getFirstName()); r = s.executeQuery(COUNT_SQL); r.next(); int rows1 = r.getInt(1); assertEquals(rows0, rows1); User goofy = new User("Goofy", "Goofy"); goofy.setId(8); dao.saveOrUpdate(goofy); actual = dao.getById(8); assertEquals(goofy.getFirstName(), actual.getFirstName()); r = s.executeQuery(COUNT_SQL); r.next(); int rows2 = r.getInt(1); assertEquals(rows1 + 1, rows2); connection.close(); dao.saveOrUpdate(goofy); }
@Override public void saveOrUpdate(IUser user) { try { PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, user.getId()); ResultSet r = ps.executeQuery(); if (r.next()) { PreparedStatement update = connection.prepareStatement(UPDATE_SQL); update.setString(1, user.getFirstName()); update.setString(2, user.getName()); update.setInt(3, user.getId()); update.execute(); connection.commit(); update.close(); } else { PreparedStatement insert = connection.prepareStatement(INSERT_SQL); insert.setInt(1, user.getId()); insert.setString(2, user.getFirstName()); insert.setString(3, user.getName()); insert.execute(); connection.commit(); insert.close(); } r.close(); ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } }
SQLUserDAO implements UserDAO { @Override public void saveOrUpdate(IUser user) { try { PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, user.getId()); ResultSet r = ps.executeQuery(); if (r.next()) { PreparedStatement update = connection.prepareStatement(UPDATE_SQL); update.setString(1, user.getFirstName()); update.setString(2, user.getName()); update.setInt(3, user.getId()); update.execute(); connection.commit(); update.close(); } else { PreparedStatement insert = connection.prepareStatement(INSERT_SQL); insert.setInt(1, user.getId()); insert.setString(2, user.getFirstName()); insert.setString(3, user.getName()); insert.execute(); connection.commit(); insert.close(); } r.close(); ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
SQLUserDAO implements UserDAO { @Override public void saveOrUpdate(IUser user) { try { PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, user.getId()); ResultSet r = ps.executeQuery(); if (r.next()) { PreparedStatement update = connection.prepareStatement(UPDATE_SQL); update.setString(1, user.getFirstName()); update.setString(2, user.getName()); update.setInt(3, user.getId()); update.execute(); connection.commit(); update.close(); } else { PreparedStatement insert = connection.prepareStatement(INSERT_SQL); insert.setInt(1, user.getId()); insert.setString(2, user.getFirstName()); insert.setString(3, user.getName()); insert.execute(); connection.commit(); insert.close(); } r.close(); ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } } SQLUserDAO(Connection c); }
SQLUserDAO implements UserDAO { @Override public void saveOrUpdate(IUser user) { try { PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, user.getId()); ResultSet r = ps.executeQuery(); if (r.next()) { PreparedStatement update = connection.prepareStatement(UPDATE_SQL); update.setString(1, user.getFirstName()); update.setString(2, user.getName()); update.setInt(3, user.getId()); update.execute(); connection.commit(); update.close(); } else { PreparedStatement insert = connection.prepareStatement(INSERT_SQL); insert.setInt(1, user.getId()); insert.setString(2, user.getFirstName()); insert.setString(3, user.getName()); insert.execute(); connection.commit(); insert.close(); } r.close(); ps.close(); } 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 void saveOrUpdate(IUser user) { try { PreparedStatement ps = connection.prepareStatement(GET_BY_ID_SQL); ps.setInt(1, user.getId()); ResultSet r = ps.executeQuery(); if (r.next()) { PreparedStatement update = connection.prepareStatement(UPDATE_SQL); update.setString(1, user.getFirstName()); update.setString(2, user.getName()); update.setInt(3, user.getId()); update.execute(); connection.commit(); update.close(); } else { PreparedStatement insert = connection.prepareStatement(INSERT_SQL); insert.setInt(1, user.getId()); insert.setString(2, user.getFirstName()); insert.setString(3, user.getName()); insert.execute(); connection.commit(); insert.close(); } r.close(); ps.close(); } 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);  }