Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 917
Santiago Alvarez Avatar asked Aug 18 '26 00:08

Santiago Alvarez


1 Answers

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;
    }
}
like image 176
Santiago Alvarez Avatar answered Aug 20 '26 14:08

Santiago Alvarez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!