-1

所以我正在制作一个简单的 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;
}

从 QUESTION 表中查询问题列表以获取调查 ID 的私有方法

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

谢谢你

4

1 回答 1

1

看你的方法:

private void findQuestionsBySurvey(JHAKSurvey survey){

您想获得调查的问题,但该方法返回无效。让它返回一个List<Question>. 在方法体中,遍历结果集,将每一行转换为一个问题,将问题添加到 aList<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);
    }

}
于 2012-07-21T17:12:35.850 回答