ResultSet next()只返回一行

时间:2016-01-19 09:21:57

标签: jdbc resultset multiple-results

我有这个方法

require.ensure

getDBConnection()方法是     private static Connection getDBConnection(){

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);
        }

我的问题是resultSet.next()只返回DB的第一行。我确定我有多行。我看到JDBC ResultSet is giving only one row although there are many rows in table?这个问题,但它确实没有回答我的问题:)

1 个答案:

答案 0 :(得分:0)

我很抱歉这个问题。我发现了自己的错误。 ResultSet next()方法工作正常,但我在addParticipantsIdToCourse(course)方法中更改了它的值:))