多行,包裹在一个对象中

时间:2013-10-08 07:45:13

标签: java sql

这是对我上一个问题的跟进,建立我的动态调查问卷。

所以我有查询加入动态创建页面所需的所有表。

结构如下(不能上传图片,没有足够的代表)

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格式的硬编码数据。 所以我的问题是,从我的查询返回的resultSet,我想在List或其他一些集合中包装所有相关的答案,这样我就可以按上述方式填充XML。

这是Im当前设置的方式,它为每个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';

谢谢你们。

1 个答案:

答案 0 :(得分:0)

也许我错了,但是如果使用地图问题问题&gt;问题然后做这样的事情呢?

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个查询(一个用于问题,一个用于可能的答案),或者在您已经创建问题时忽略问题数据,并且只添加其他答案。