Exemplo n.º 1
0
  public void delete(Student student) throws Exception {
    Connection connection = this.dataSource.getConnection();

    for (Exam exam : student.getExams()) {
      // TODO voglio eliminare tutto
      this.examsRepository.delete(exam, student);
    }

    String insert = "delete from students where code = ?";

    PreparedStatement statement = connection.prepareStatement(insert);
    statement.setInt(1, student.getCode());
    statement.executeUpdate();

    // release resources
    statement.close();
    connection.close();
  }
Exemplo n.º 2
0
  public void persist(Student student) throws SQLException {
    Connection connection = this.dataSource.getConnection();
    PreparedStatement statement = null;
    try {
      if (findByPrimaryKey(student.getCode()) != null) {
        System.err.println("the student already exists");
        return;
      }

      String insert = "insert into students(code, firstname, lastname, birthDate) values (?,?,?,?)";

      statement = connection.prepareStatement(insert);
      statement.setInt(1, student.getCode());
      statement.setString(2, student.getFirstName());
      statement.setString(3, student.getLastName());

      Date t = new Date();
      t.getTime();
      statement.setDate(4, new java.sql.Date(student.getBirthDate().getTime()));
      statement.executeUpdate();

      for (Exam exam : student.getExams()) {
        // TODO se ci sono degli errori qui? che succede?
        // Come possiamo fare in modo che: o inserisco tutti gli esami e
        // lo studente o niente?
        this.examsRepository.persist(exam, student);
      }
      // TODO
      // Che succede se eccezione prima?
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      statement.close();
      connection.close();
    }
  }