Exemplo n.º 1
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;
  }
Exemplo n.º 2
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<>();
    }
  }