コード例 #1
0
 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;
 }
コード例 #2
0
 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);
   }
 }
コード例 #3
0
 @Override
 public void saveOrUpdate(Company company) throws DaoException {
   if (findById(company.getId()) == null) {
     save(company);
   } else {
     update(company);
   }
 }
コード例 #4
0
 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);
   }
 }