【问题标题】:ResultSet next() is returning only one rowResultSet next() 只返回一行
【发布时间】:2016-04-24 16:39:39
【问题描述】:

我有这个方法

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

getDBConnection() 方法是 私有静态连接 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;

}

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

【问题讨论】:

  • 你怎么知道它只返回一个?调试?一些输出?
  • 您确定连接到正确的数据库吗?
  • 当我调试时,我看到只有一门课程被添加到我的 ArrayList 中。是的,我非常确定数据库

标签: jdbc resultset multiple-results


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    相关资源
    最近更新 更多