Esempio n. 1
0
  @Override
  public void actualizar(Etapa objeto) throws BusinessException {

    Connection con = ConexionJdbc.getConnection();

    String sql = "UPDATE etapa SET km = ?, salida = ?, llegada = ?, dorsal = ? WHERE netapa = ?";

    PreparedStatement pstm = null;
    try {

      pstm = con.prepareStatement(sql);
      pstm.setInt(1, objeto.getKm());
      pstm.setString(2, objeto.getSalida());
      pstm.setString(3, objeto.getLlegada());
      pstm.setInt(4, objeto.getDorsal());
      pstm.setInt(5, objeto.getNetapa());

      pstm.executeUpdate();

    } catch (SQLException e) {
      e.printStackTrace();
      throw new BusinessException("La actualización no ha sido posible");
    } finally {

      ConexionJdbc.cerrar(pstm);
    }
  }
Esempio n. 2
0
  public void insertar(List<Etapa> l) throws BusinessException {

    Etapa e = null;

    PreparedStatement pstm = null;

    ResultSet rs = null;

    String sql = "insert into etapa(km, salida, llegada, dorsal)values(?,?,?,?)";

    boolean valorAutocomitInicio = ConexionJdbc.getAutoCommit();
    ConexionJdbc.setAutoCommit(false);

    try {

      Connection con = ConexionJdbc.getConnection();
      int registro = 0;
      for (int i = 0; i < l.size(); i++) {

        e =
            new Etapa(
                null,
                l.get(registro).getKm(),
                l.get(registro).getSalida(),
                l.get(registro).getLlegada(),
                l.get(registro).getDorsal());
        pstm = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);

        if (pstm.executeUpdate() != 0) {

          rs = pstm.getGeneratedKeys();
          rs.first();
          int netapa = rs.getInt(1);
          e.setNetapa(netapa);
          pstm.setInt(1, e.getKm());
          pstm.setString(2, e.getSalida());
          pstm.setString(3, e.getLlegada());
          pstm.setInt(1, e.getDorsal());
        }

        pstm.executeUpdate();
        registro++;
      }

      ConexionJdbc.commit();

    } catch (SQLException ex) {

      ConexionJdbc.rollback();

      ex.printStackTrace();
      throw new BusinessException("La inserción de las etapas  no ha sido posible");
    } finally {

      ConexionJdbc.setAutoCommit(valorAutocomitInicio);

      ConexionJdbc.cerrar(pstm);
    }
  }