title: resultset iterating only one time on while loop
body: <p>So im accessing a mySql database in my java code using a <code>Statement</code> to execute my query and save the returning <code>ResultSet</code> in a <code>ResultSet</code> object then using that same <code>ResultSet</code> object, i iterate over the rows from the result and simply print out the data for every row. </p>
  
  <p>Here is the code:</p>
  
  <pre><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&lt;Semester&gt; getSemesters(){
      try{
          ArrayList&lt;Semester&gt; semesters = new ArrayList&lt;Semester&gt;();
          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;
  }
  </code></pre>
  
  <p>My problem is that in the while loop, the <code>rs.next()</code> 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.</p>
  
  <blockquote>
    <p>When debugging, if i inspect <code>rs.next()</code> 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.</p>
  </blockquote>
  
  <p>Thank you very much for your help.</p>