Results are repeated only once during the cycle

This way I access the mySql database in my java code using Statement

to execute my query and store the returned ResultSet

in an object ResultSet

, then using the same object ResultSet

, iterate through the rows from the result and just print the data for each 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 statement rs.next()

only returns true on the first iteration. In other words, it only prints the first row of my table in the database when there are actually many rows.

When debugging, if I check rs.next()

before running it, when I actually run it on the first iteration, it goes into a loop and prints out the second row of my table.

Many thanks for your help.

+3


source to share


2 answers


My problem was that in the method getClasses()

I am also modifying the variable rs

and therefore in the while loop the variable is not the same, so it returns false.

the solution is to make the variable ResultSet rs

local in each 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;
    }
}

      

0


source


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

      



0


source







All Articles