-1

I am having this method

public List<Course> getCourses() throws InvalidCourseDataException {
    ArrayList<Course> allCoursesFromDB = new ArrayList<>();
    Connection dbConnection = null;

    String getFromTableSQL = "SELECT * FROM courses";

    try {
        dbConnection = getDBConnection();
        statement = dbConnection.createStatement();
        resultSet = statement.executeQuery(getFromTableSQL);

        while (resultSet.next()) {

            String courseCategory = resultSet.getString("course_category");
            String courseTitle = resultSet.getString("course_title");

            int courseId = resultSet.getInt("course_id");

            Date startDate = resultSet.getTimestamp("starting_date");
            Date endDate = resultSet.getDate("ending_date");
            String description = resultSet.getString("description");
            int teacherId = resultSet.getInt("teacher_id");

            Course course = new Course(courseCategory, courseTitle, startDate, endDate);
            course.setCourseId(courseId);
            course.setTeacherId(teacherId);
            course.setDescription(description);

            addParticipantsIdToCourse(course);
            allCoursesFromDB.add(course);
        }

The getDBConnection() method is private static Connection getDBConnection() {

    System.out.println("-------- MySQL JDBC Connection Testing ------------");

    Connection dbConnection = null;

    try {
        Class.forName(DB_DRIVER);
    } catch (ClassNotFoundException e) {
        System.out.println(e.getMessage());
    }

    try {
        dbConnection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
        return dbConnection;
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }

    return dbConnection;

}

My problem is that resultSet.next() is returning only the first row from DB. I'am sure that I have multiple rows. I saw JDBC ResultSet is giving only one row although there are many rows in table? that question but it really doesn't answer my :)

Community
  • 1
  • 1
Ivaylo Pankov
  • 99
  • 1
  • 5

1 Answers1

0

I am sorry for the question. I found my mistake. ResultSet next() method is working fine, but I've changed its value in my addParticipantsIdToCourse(course) method :)

Ivaylo Pankov
  • 99
  • 1
  • 5