如何在Java JDBC中显示resultset中的arrayList内容?

时间:2012-07-21 17:01:33

标签: jdbc

所以我正在制作一个简单的java项目,在glassfish中使用JDBC,看看它是如何工作的。该程序只显示调查列表和您选择的调查问题列表。但是我似乎无法显示我选择的调查问题列表。我一直在变空。这些是我创建的方法:

将结果集转换为对象模型数据值

public JHAKSurvey findSurvey(long id) {
    System.out.println("JDBC: FIND SURVEY");
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    JHAKSurvey survey = null;
    try {
        connection = openConnection();
        String query = "SELECT * FROM APP.SURVEY WHERE ID=?";
        ps = connection.prepareStatement(query);
        ps.setLong(1, id);
        rs = ps.executeQuery();
        while (rs.next()) {
            survey = createSurveyFromResultSet(rs);             
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeConnection(connection);
    }
    return survey;
}

查询调查ID的QUESTION表中的问题列表的私有方法

private void findQuestionsBySurvey(JHAKSurvey survey){
    System.out.println("JDBC: FIND QUESTIONS BY SURVEY");
    Connection connection = null;
    PreparedStatement ps = null;

    try {
        connection = openConnection();
        String query = "SELECT * FROM APP.QUESTION WHERE SURVEYID=?";
        ps = connection.prepareStatement(query);
        ps.setLong(1, survey.getId());
        ps.executeQuery(query);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeConnection(connection);
    }

}

将查找结果集列表转换为问题对象并将其添加到调查对象的私有方法

private void createQuestionFromResultSet(ResultSet rs, JHAKSurvey survey){
    ArrayList<JHAKQuestion> qList = new ArrayList<JHAKQuestion>();      
    JHAKQuestion question = new JHAKQuestion();
    JHAKSurvey ss = new JHAKSurvey();
    //qList.add(survey.getQuestions());

    try {
        while (rs.next()) {
            //question.setDescription(qList.toString());
            question.setId(rs.getLong("ID"));
            question.setDescription(rs.getString("DESCRIPTION"));
            qList.add(question);
            survey.setQuestions(qList);
        }               
        System.out.println("createQuestionFromResultSet : JDBC : successful");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        System.out.println("createQuestionFromResultSet : JDBC : fail");
        e.printStackTrace();
    }
}

将结果集转换为调查对象的私有方法。

    private JHAKSurvey createSurveyFromResultSet(ResultSet rs){
    JHAKSurvey survey = new JHAKSurvey();
    Boolean active = false;
    String yes;
    try {
        yes = rs.getString("ACTIVE");
        survey.setId(rs.getLong("ID"));
        survey.setTitle(rs.getString("TITLE"));
        if (yes.equals(Character.toString('Y'))) {
            survey.setActive(true);
        } else {
            survey.setActive(false);
        }   
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return survey;
}

我错过了什么?我似乎也得到错误:

cannot convert from void to JHAKQuestion

当我尝试方法时:createQuestionFromResultSet();

谢谢

1 个答案:

答案 0 :(得分:1)

看看你的方法:

private void findQuestionsBySurvey(JHAKSurvey survey){

您想获得调查问题,但该方法返回无效。让它返回List<Question>。在方法体中,遍历结果集,将每一行转换为一个问题,将问题添加到List<Question>,然后返回此列表。

或者,如果方法的目标是将问题添加到作为参数传递的调查中,则将方法重命名为

private void addQuestionsToSurvey(JHAKSurvey survey) {

并且,在方法体内,调用方法createQuestionFromResultSet(应该命名为createQuestionsFromResultSetAndAddThemToSurvey),结果集和调查作为参数:

private void findQuestionsBySurvey(JHAKSurvey survey){
    System.out.println("JDBC: FIND QUESTIONS BY SURVEY");
    Connection connection = null;
    PreparedStatement ps = null;

    try {
        connection = openConnection();
        String query = "SELECT * FROM APP.QUESTION WHERE SURVEYID=?";
        ps = connection.prepareStatement(query);
        ps.setLong(1, survey.getId());
        ResultSet rs = ps.executeQuery(query);
        createQuestionFromResultSet(survey); 
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeConnection(connection);
    }

}