Пример #1
0
  @Override
  public void update(Student updatedStudent) throws SQLException {
    PreparedStatement preparedStatement =
        connection.prepareStatement("UPDATE Student SET name=? WHERE id=?");
    preparedStatement.setString(1, updatedStudent.getName());
    preparedStatement.setInt(2, updatedStudent.getId());

    preparedStatement.executeUpdate();
  }
Пример #2
0
  @Override
  public void insert(Student student) throws SQLException {
    PreparedStatement preparedStatement =
        connection.prepareStatement("INSERT INTO Student (id,name) VALUES (?,?)");
    preparedStatement.setInt(1, student.getId());
    preparedStatement.setString(2, student.getName());

    preparedStatement.executeUpdate();
  }
Пример #3
0
  @Override
  public Student findById(int id) {

    Student student = new Student();
    try {
      PreparedStatement preparedStatement =
          connection.prepareStatement("SELECT name FROM Student WHERE student_id=?");
      preparedStatement.setInt(1, id);
      ResultSet resultSet = preparedStatement.executeQuery();
      while (resultSet.next()) {
        student.setName(resultSet.getString("name"));
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }

    return student;
  }
Пример #4
0
  @Override
  public List<Student> findAll() {
    try {
      PreparedStatement preparedStatement =
          connection.prepareStatement("SELECT id,name AS nama FROM Student");
      ResultSet resultSet = preparedStatement.executeQuery();

      List<Student> studentList = new ArrayList<>();
      while (resultSet.next()) {
        Student student = new Student();

        student.setId(resultSet.getInt("id"));
        student.setName(resultSet.getString("nama"));

        studentList.add(student);
      }

      return studentList;

    } catch (SQLException e) {
      e.printStackTrace();
      return new ArrayList<>();
    }
  }