简体   繁体   中英

resultset iterating only one time on while loop

So im accessing a mySql database in my java code using a Statement to execute my query and save the returning ResultSet in a ResultSet object then using that same ResultSet object, i iterate over the rows from the result and simply print out the data for every row.

Here is the code:

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

My problem is that in the while loop, the rs.next() statement is only returning true on the first iteration. In other words, it only prints the first row of my table in the database when actually having many rows.

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.

Thank you very much for your help.

My problem was that in the getClasses() method i also modify the rs variable and so in the while loop the variable is not the same so it returns false.

the solution is to make the ResultSet rs variable local in every method.

like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM