ResultSet未填充结果

时间:2011-11-21 20:32:59

标签: java oracle jdbc

我试图从Oracle数据库中提取结果。我编写了一个正确的查询,并在sqlplus中手动发出时生成准确的结果。此外,当查询仅匹配一行时,代码按预期工作(换句话说,当ResultSet只有一行时,一切正常)。但是,当多个行与查询匹配时,Oracle JDBC返回的ResultSet为空。

public Component[] getAllComponents(int typeId, int osId) throws SQLException
{
    String query= "SELECT c.component_id, c.component_name, c.component_version, c.type_id, c.post_download_instructions, "
                + "o.os_id, o.os_name, o.description AS os_description, "
                + "i.file_location, i.release_date, i.patch_number, i.file_id, "
                + "i.description AS i_description "
                + "FROM components c, installation_files i, operating_systems o "
                + "WHERE c.type_id = ? " 
                + "AND i.os_id = ? " 
                + "AND c.component_id = i.component_id "
                + "AND i.os_id = o.os_id";

    ResultSet results       = null;
    PreparedStatement stmt  = null;
    ArrayList<Component> found = new ArrayList<Component>();

    try {
        stmt = dbConn.prepareStatement(query); //dbConn is member variable

        stmt.setInt(1, typeId);
        stmt.setInt(2, osId);

        results = stmt.executeQuery();

        while(results.next()){
            //Some logic
        }

    } finally {
        if(results != null) results.close();
        if(stmt != null) stmt.close();
        dbConn.close();
    }

    //More Code
    //etc. etc.

检查ResultSet表明,当获取的结果应包含多行时,调用ResultSet.next()永远不会生成true。但是,手动发出查询会产生结果,当只返回一行时,一切正常。有谁知道发生了什么?我正在使用Oracle的ojdbc6.jar。

谢谢!

1 个答案:

答案 0 :(得分:0)

在该查询之前,您可以检查是否确实有一些组件包含COUNT(*)而不是所有字段。然后,仅当COUNT(*)为一个或多个时才运行您的查询。