static Company companyFromTuple(ResultSet resultSet) throws SQLException { Company company = new Company(); long id = resultSet.getLong("company.id"); String name = resultSet.getString("company.name"); company.setId(id); company.setName(name); return company; }
private void update(Company company) throws DaoException { PreparedStatement statement = null; try { statement = JdbcUtils.getConnection().prepareStatement(UPDATE); statement.setString(1, company.getName()); statement.setLong(2, company.getId()); statement.execute(); } catch (SQLException e) { logger.warn(e.getMessage()); throw new DaoException(e); } finally { JdbcUtils.closeStatement(statement); } }
@Override public void saveOrUpdate(Company company) throws DaoException { if (findById(company.getId()) == null) { save(company); } else { update(company); } }
private void save(Company company) throws DaoException { PreparedStatement statement = null; ResultSet resultKey = null; try { statement = JdbcUtils.getConnection().prepareStatement(SAVE, Statement.RETURN_GENERATED_KEYS); statement.setString(1, company.getName()); statement.execute(); resultKey = statement.getGeneratedKeys(); resultKey.next(); long id = resultKey.getLong(1); company.setId(id); } catch (SQLException e) { logger.warn(e.getMessage()); throw new DaoException(e); } finally { JdbcUtils.closeResultSet(resultKey); JdbcUtils.closeStatement(statement); } }