结果集在 while 循环中只迭代一次

resultset iterating only one time on while loop

所以我在 java 代码中使用 Statement 访问 mySql 数据库来执行我的查询并将返回的 ResultSet 保存在 ResultSet对象然后使用相同的 ResultSet 对象,我遍历结果中的行并简单地打印出每一行的数据。

代码如下:

public class DBConnect {
    private Connection conn;
    private Statement st;
    private ResultSet rs;

    public DBConnect(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/unal_grades", "root", "");
            st = conn.createStatement();
        }catch(Exception ex){
            System.out.println("Error: " + ex);
        }
    }

public ArrayList<Semester> getSemesters(){
    try{
        ArrayList<Semester> semesters = new ArrayList<Semester>();
        String query = "Select * from semester";
        rs = st.executeQuery(query);
        System.out.println("semesters");
        while(rs.next()){
            int id = rs.getInt("id");
            String name = rs.getString("name");
            float average = rs.getFloat("average");
            boolean active = rs.getBoolean("active");

            System.out.println(name+" / "+average+" / "+active);
            Semester semester = new Semester(id, name, average, active);
            semester.setClasses(getClasses(semester));
            semesters.add(semester);
        }
        System.out.println();
        return semesters;
    }catch(Exception ex){
        System.out.println("Error: " + ex);
    }
    return null;
}

我的问题是,在 while 循环中,rs.next() 语句仅在第一次迭代时返回 true。换句话说,当实际有很多行时,它只打印数据库中 my table 的第一行。

When debugging, if i inspect rs.next() before actually running over it, when i actually run over it on the first iteration, it goes into the loop and prints out the second row of my table.

非常感谢您的帮助。

Statement st;
ResultSet rs;
try{
  String query = "Select * from semester";
  st = con.createStatement();
  rs = st.executeQuery(query);
  System.out.println("semesters");

  while(rs.next()){
    int id = rs.getInt("id");
    String name = rs.getString("name");
    float average = rs.getFloat("average");
   boolean active = rs.getBoolean("active");

    System.out.println(name+" / "+average+" / "+active);
}

System.out.println();
} catch(Exception ex){
   System.out.println("Error: " + ex);
}  

我的问题是,在 getClasses() 方法中,我还修改了 rs 变量,因此在 while 循环中变量不相同,所以它 returns false。

解决方案是在每个方法中使 ResultSet rs 变量成为局部变量。

像这样:

public class DBConnect {
    private Connection conn;

    public DBConnect(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/unal_grades", "root", "");
        }catch(Exception ex){
             System.out.println("Error: " + ex);
        }
    }

    public ArrayList<Semester> getSemesters(){
        ResultSet rs;
        try{
            Statement st = conn.createStatement();
            ArrayList<Semester> semesters = new ArrayList<Semester>();
            String query = "Select * from semester";
            rs = st.executeQuery(query);
            System.out.println("semesters");
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                float average = rs.getFloat("average");
                boolean active = rs.getBoolean("active");

                System.out.println(name+" / "+average+" / "+active);
                Semester semester = new Semester(id, name, average, active);
                semester.setClasses(getClasses(semester));
                semesters.add(semester);
            }
            System.out.println();
            return semesters;
        }catch(Exception ex){
            System.out.println("Error: " + ex);
        }
        return null;
    }
}