Exemplo n.º 1
0
  @Override
  public void update(User user) throws DbException {
    PreparedStatement ps = null;
    try {
      prepareConnection();

      ps = connection.prepareStatement(USERS_UPDATE);
      ps.setString(1, user.getFirstName());
      ps.setString(2, user.getLastName());
      ps.setString(3, user.getNickName());
      ps.setString(4, user.getEmail());
      ps.setInt(5, user.getRoleId());
      ps.setString(6, user.getPassword());
      ps.setDate(7, user.getHiredDay());
      ps.setDate(8, user.getFiredDay());
      ps.setString(9, user.getUserNote());
      ps.setString(10, user.getStatus().toDataBaseVal());
      ps.setString(11, user.getState().toDataBaseVal());
      ps.setInt(12, user.getUserId());

      ps.executeUpdate();

    } catch (SQLException e) {
      throw new DbException("Can't execute SQL = '" + USERS_UPDATE + "'", e);
    } finally {
      daoHelper.closeDataBaseEntities(ps, null, connection);
    }
  }
Exemplo n.º 2
0
  @Override
  public long insert(User user) throws DbException {
    PreparedStatement ps = null;
    long result;
    try {
      prepareConnection();

      ps = connection.prepareStatement(USERS_INSERT, Statement.RETURN_GENERATED_KEYS);
      ps.setString(1, user.getFirstName());
      ps.setString(2, user.getLastName());
      ps.setString(3, user.getNickName());
      ps.setString(4, user.getEmail());
      ps.setInt(5, user.getRoleId());
      ps.setString(6, user.getPassword());
      ps.setDate(7, user.getHiredDay());
      ps.setDate(8, user.getFiredDay());
      ps.setString(9, user.getUserNote());
      ps.setString(10, user.getStatus().toDataBaseVal());
      ps.setString(11, user.getState().toDataBaseVal());

      ps.executeUpdate();

      try (ResultSet rs = ps.getGeneratedKeys()) {
        if (rs.next()) {
          result = rs.getLong(1);
        } else {
          throw new DbException(
              "Inserting failed on query: '" + USERS_INSERT + "' no ID obtained.");
        }
      }
    } catch (SQLException e) {
      throw new DbException("Can't execute SQL = '" + USERS_INSERT + "'", e);
    } finally {
      daoHelper.closeDataBaseEntities(ps, null, connection);
    }

    return result;
  }