需要在每次重新安排时关闭准备好的声明,否则我最终可以做到一次

时间:2014-07-08 07:12:50

标签: jdbc prepared-statement

    ResultSet rst = stmt.executeQuery();
    PreparedStatement preparedStatement;
    while (rst.next()) {
        Integer languageId = rst.getInt("language_id");
        Map<String, List<UserMessageItem>> listUserMessageItem = new HashMap<String, List<UserMessageItem>>();
        *preparedStatement = connection.prepareStatement(query);*// first assignation of preparedStatement
        preparedStatement.setInt(1, languageId);
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            //do stuff
            } else {
                //do else stuff

    }

    *preparedStatement = connection.prepareStatement(DictionaryManager.LOAD_MAX_MESSAGE_ID);*//other assignation of preparedStatement
    ResultSet resultSet = preparedStatement.executeQuery();
    if (resultSet.next()) {
        maxMessageId = resultSet.getInt("MAX(message_id)");
    }

    resultSet.close();
    preparedStatement.close();

    rst.close();
    stmt.close();

我必须重构这些东西......请不要关心尝试捕获的遗漏最终阻止...任何我想问你:我应该在分配新的prepareStatement之前关闭preparedStatement吗?

1 个答案:

答案 0 :(得分:2)

每次都必须关闭它。重新分配它会创建一个新的并释放旧的。