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

时间:2015-07-03 06:15:01

标签: java mysql jdbc resultset

所以我使用Statement在我的java代码中访问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。换句话说,它只在实际拥有多行时打印数据库中我表的第一行。

  

调试时,如果我在实际运行它之前检查rs.next(),当我在第一次迭代时实际运行它时,它进入循环并打印出我表的第二行。

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

我的问题是在getClasses()方法中我也修改了rs变量,因此在while循环中变量不一样所以返回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;
    }
}

答案 1 :(得分:0)

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