'nest' ResultSet 查询不可能吗?或在循环内使用查询?

Is it NOT possible to 'nest' ResultSet queries? or use a query inside a loop?

public void generateEnumeration() {
        StyledDocument docExam = txtpaneExamGeneration.getStyledDocument();
        StyledDocument docAnsKey = txtpaneAnswerKey.getStyledDocument();

        Connection con = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite");
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT DISTINCT group_name FROM active_items;");
            int num = numIndex;
            char[] letterArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
            int letterIndex = 0;

            docExam.insertString(docExam.getLength(), "Enumeration - Enumerate the following:\n\n" , null);

            while(rs.next()){
                String groupNameHandler = rs.getString("group_name");

                docExam.insertString(docExam.getLength(), letterArray[letterIndex] + ". " + groupNameHandler + ".\n", null);

                ResultSet rs2 = stmt.executeQuery("SELECT * FROM active_items WHERE group_name = '" + groupNameHandler + "'");
                while(rs2.next()) {
                    String itemNumbering = Integer.toString(num);
                    String ansHandler = rs.getString("item_name");

                    docAnsKey.insertString(docAnsKey.getLength(), itemNumbering + ". ", null);
                    docAnsKey.insertString(docAnsKey.getLength(), ansHandler + "\n", null);

                    num ++; // this is needed to increase the numbering while in the WHILE LOOP
                    numIndex ++; // this is needed to save the last number used after the WHILE LOOP so that it will be continued on the next function
                } // end for ResultSet 2

                letterIndex ++;

            } // end ResultSet 1

            letterIndex = 0;
            docExam.insertString(docExam.getLength(), "\n\n" , null);

        } catch (ClassNotFoundException | SQLException e) {
            System.out.println(e);
        } catch (BadLocationException ex) {
            Logger.getLogger(generateExam.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                con.close();
                stmt.close();
            } catch (SQLException ex) {
                Logger.getLogger(generateExam.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

此代码的目的是生成枚举类型的测验。这将输出到 2 个 JTextPanes,一个用于 docExam,一个用于 docAnsKey

ResultSet rs = stmt.executeQuery("SELECT DISTINCT group_name FROM active_items;"); 应该(确实如此,因为我在 SQLite 上测试过并且它有效)从数据库中检索 3 行。然而,输出是这样的:

docExam:

Enumeration - Enumerate the following

A. Enum question 1 

in docAnsKey:

1. CorrectAns1
2. CorrectAns2

实际上,数据库中有 12 个项目。

似乎while(rs.next()){只执行了一次,这就是为什么跳过其余代码的原因。而且我在这里找不到我的错误在哪里。有人可以指出给我吗?

数据库内容:

item_id --- item_name    --- group_name
1            CorrectAns1     Enum question 1
2            CorrectAns2     Enum question 1
3            CorrectAns3     Enum question 2
4            CorrectAns4     Enum question 2
5            CorrectAns5     Enum question 2
6            CorrectAns6     Enum question 2
7            CorrectAns7     Enum question 2
8            CorrectAns8     Enum question 2
9            CorrectAns9     Enum question 2
10           CorrectAns10    Enum question 2
11           CorrectAns11    Enum question 3
12           CorrectAns12    Enum question 3

经过几个小时的反复试验,我得出结论,ResultSet 无法处理多个 'uses'。因此,当我将它用于 rs2.next() 时,它忘记了来自 rs.next() 的数据。所以我的解决方法是将函数的整个第二部分从 while 循环中取出,因为顺序类似于我调用 SELECT DISTINCT group_name FROM active_items;SELECT * FROM active_items 并且不会影响程序的完整性。

最终代码在这里:

public void generateEnumeration() {
        StyledDocument docExam = txtpaneExamGeneration.getStyledDocument();
        StyledDocument docAnsKey = txtpaneAnswerKey.getStyledDocument();

        Connection con = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite");
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT DISTINCT group_name FROM active_items;");
            int num = numIndex;
            char[] letterArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
            int letterIndex = 0;

            docExam.insertString(docExam.getLength(), "Enumeration - Enumerate the following:\n\n" , null);

            while(rs.next()){
                String groupNameHandler = rs.getString("group_name");

                docExam.insertString(docExam.getLength(), letterArray[letterIndex] + ". " + groupNameHandler + ".\n", null);

                letterIndex ++;
            }

            ResultSet rs2 = stmt.executeQuery("SELECT * FROM active_items");
            while(rs2.next()) {
                String itemNumbering = Integer.toString(num);
                String ansHandler = rs.getString("item_name");

                docAnsKey.insertString(docAnsKey.getLength(), itemNumbering + ". ", null);
                docAnsKey.insertString(docAnsKey.getLength(), ansHandler + "\n", null);

                num ++; // this is needed to increase the numbering while in the WHILE LOOP
                numIndex ++; // this is needed to save the last number used after the WHILE LOOP so that it will be continued on the next function
            }

            letterIndex = 0;
            docExam.insertString(docExam.getLength(), "\n\n" , null);

        } catch (ClassNotFoundException | SQLException e) {
            System.out.println(e);
        } catch (BadLocationException ex) {
            Logger.getLogger(generateExam.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                con.close();
                stmt.close();
            } catch (SQLException ex) {
                Logger.getLogger(generateExam.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }