0

这是我最后一个问题的后续,建立我的动态问卷。

所以我有查询连接我需要动态创建页面的所有表。

结构如下(不能上传图片,rep不够)

Question    QuestionType        Possible answer 
-------------------------------------------
How far     Single Choice       Too Far 
How far     Single Choice       Not too far 
How long    Multiple Choice     Way long
How long    Multiple Choice     Huh?
How long    Multiple Choice     Dumb question   

查询为每个可能的答案返回一行,有时为 2,有时为 4。没有限制单个问题可以有多少选项。

在我的项目中,我有一个 XML 结构,我的应用程序使用它来构建页面。

<rowset>
    <row>
        <questionText>This is a single choice question*</questionText>
        <questionType>Single Choice</questionType>
        <possibleAnswerSet>
            <possibleAnswer>Yes</possibleAnswer>
            <possibleAnswer>No</possibleAnswer>
            <possibleAnswer>Maybe</possibleAnswer>
        </possibleAnswerSet>
    </row>
    <row>
        <questionText>This is a survey question</questionText>
        <questionType>Survey</questionType>
        <possibleAnswerSet>
            <possibleAnswer></possibleAnswer>
        </possibleAnswerSet>
    </row>
    <row>
        <questionText>This is a mutiple choice question*</questionText>
        <questionType>Multiple Choice</questionType>
        <possibleAnswerSet>
            <possibleAnswer>Apples</possibleAnswer>
            <possibleAnswer>Bananas</possibleAnswer>
            <possibleAnswer>Cranberries</possibleAnswer>
            <possibleAnswer>Dragon fruit</possibleAnswer>
        </possibleAnswerSet>
    </row>
</rowset>

这件作品完美,数据在 XML 中硬编码,我的页面生成。所以我的问题是,从我的查询返回的结果集中,我想将所有相关的答案包装在 List 或其他一些集合中,以便我可以如上所述填充 XML。

这就是我当前设置的方式,它为每个 POSSIBLE_ANSWER 返回一个对象,而不是每个问题

try 
        {
            while (rset.next()) 
            {
                ClientPojo clientObj = new ClientPojo();

                questionOptions = rset.getInt("QUESTION_OPTIONS");
                passPercentage = rset.getInt("CATEGORY_PASS_PERCENTAGE");
                questionText = rset.getString("QUESTION_TEXT");
                questionType = rset.getString("QUESTION_TYPE");
                categoryName = rset.getString("CATEGORY_NAME");
                possibleAnswerText = rset.getString("POSSIBLE_ANSWER_TEXT");
                possibleAnswerCorrect = rset.getString("POSSIBLE_ANSWER_CORRECT");
                questionId = rset.getInt("QUESTION_ID");

                clientObj.setAssessmentAreaId(assessmentAreaId);
                clientObj.setCategoryName(categoryName);
                clientObj.setPassPercentage(passPercentage);
                clientObj.setPossibleAnswerCorrect(possibleAnswerCorrect);
                clientObj.setPossibleAnswerText(possibleAnswerText);
                clientObj.setQuestionOptions(questionOptions);
                clientObj.setQuestionText(questionText);
                clientObj.setQuestionType(questionType);
                clientObj.setQuestionId(questionId);

                questionsList.add(clientObj);
            }
        }

我希望我使用的查询是什么,还是我需要改变它。

编辑:这是我的查询:

SELECT "CAQ_SCHEMA"."QUESTION".*, "CAQ_SCHEMA"."CATEGORY".*, "CAQ_SCHEMA"."CATEGORY_QUESTION_LINK".*, "CAQ_SCHEMA"."POSSIBLE_ANSWER".* , "CAQ_SCHEMA"."ASSESSMENT_AREA".* 
FROM "CAQ_SCHEMA"."QUESTION" 
    JOIN "CAQ_SCHEMA"."CATEGORY_QUESTION_LINK" 
        ON "CAQ_SCHEMA"."CATEGORY_QUESTION_LINK".QUESTION_ID = "CAQ_SCHEMA"."QUESTION".QUESTION_ID
    JOIN "CAQ_SCHEMA"."POSSIBLE_ANSWER"
        ON "CAQ_SCHEMA"."POSSIBLE_ANSWER".QUESTION_ID = "CAQ_SCHEMA"."QUESTION".QUESTION_ID
    JOIN "CAQ_SCHEMA"."CATEGORY" 
        ON "CAQ_SCHEMA"."CATEGORY".CATEGORY_ID = "CAQ_SCHEMA"."CATEGORY_QUESTION_LINK".CATEGORY_ID
    JOIN "CAQ_SCHEMA"."ASSESSMENT_AREA"
        ON "CAQ_SCHEMA"."ASSESSMENT_AREA".ASSESSMENT_AREA_ID = "CAQ_SCHEMA"."CATEGORY".ASSESSMENT_AREA_ID
    WHERE "CAQ_SCHEMA"."ASSESSMENT_AREA".ASSESSMENT_AREA_ID = '1';

多谢你们。

4

1 回答 1

0

也许我错了,但是使用地图 questionId->question 然后做这样的事情呢?

Map<Integer, ClientPojo> questions = new HashMap<Integer, ClientPojo>();

while (rset.next()) 
{
  questionId = rset.getInt("QUESTION_ID");

  ClientPojo clientObj = questions.get(questionId);
  if( clientObj == null ) 
  {
    ClientPojo clientObj = new ClientPojo();

    questionOptions = rset.getInt("QUESTION_OPTIONS");
    passPercentage = rset.getInt("CATEGORY_PASS_PERCENTAGE");
    questionText = rset.getString("QUESTION_TEXT");
    questionType = rset.getString("QUESTION_TYPE");
    categoryName = rset.getString("CATEGORY_NAME");

    clientObj.setAssessmentAreaId(assessmentAreaId);
    clientObj.setCategoryName(categoryName);
    clientObj.setPassPercentage(passPercentage);
    clientObj.setQuestionOptions(questionOptions);
    clientObj.setQuestionText(questionText);
    clientObj.setQuestionType(questionType);
    clientObj.setQuestionId(questionId);

    questionsList.add(clientObj);
  }

  possibleAnswerText = rset.getString("POSSIBLE_ANSWER_TEXT");
  possibleAnswerCorrect = rset.getString("POSSIBLE_ANSWER_CORRECT");

  //I'll leave the Answert object for you
  clientObj.addPossibleAnswer( new Answer(possibleAnswerText, possibleAnswerCorrect ) );
}

如果有多个答案,连接自然会导致多个结果集。因此,您要么使用 2 个查询(一个用于问题,一个用于可能的答案),要么在您已经创建问题时忽略问题数据并仅添加其他答案。

于 2013-10-08T11:10:21.547 回答