嵌套执行查询中的“结果集已关闭”错误

时间:2011-09-23 05:39:09

标签: java mysql sql jdbc

我想编写一个查询,该查询应该从java中的数据库中打印xml标记的值。

<employee emp:empid=" " emp:empname="" /><location loc:locname=" "/>

下面的代码给出了错误

  

“结果集已关闭”。

如何解决这个问题?

        connection = dataSource.getConnection();
    ResultSet rs;
    connection.setAutoCommit(false);
    System.out.println("Connected to server OELDBSQL!!!");
    Statement stmt = connection.createStatement();

    String querystring = "select empid,empname from empt";

    rs = stmt.executeQuery(querystring);
    Element child1 = doc.createElement("employee");
    try {
        while (rs.next()) {
            child1.setAttributeNS(emp, "emp:empid", rs.getString(1));
            child1.setAttributeNS(emp, "emp:empname", rs.getString(2));
        }
            String querystring1 = "select locname from Locate";
            ResultSet rs1;
            rs1 = stmt.executeQuery(querystring1);
            while (rs1.next()) {
                Element element = doc.createElement("location");
                child1.appendChild(element);

                element.setAttributeNS(loc, "loc:locaname", rs.getString(1));
            }
        } catch (Exception e) {
            System.out.println("Exception in connecting to DB"
                    + e.getMessage());
            System.err.println(e.getMessage());
        }
    } catch (Exception e) {
        System.out
                .println("Exception in connecting to DB" + e.getMessage());
        System.err.println(e.getMessage());
    }

2 个答案:

答案 0 :(得分:3)

使用相同的语句打开rs1之后,恕我直言这行代码

  element.setAttributeNS(loc,"loc:locaname",rs.getString(1));}  

会抛出异常,因为它正在处理旧的结果集(rs)

Statement类的Javadocs声明:

/**
 * <P>The object used for executing a static SQL statement
 * and returning the results it produces.
 * <P>
 * By default, only one <code>ResultSet</code> object per <code>Statement</code>
 * object can be open at the same time. Therefore, if the reading of one 
 * <code>ResultSet</code> object is interleaved
 * with the reading of another, each must have been generated by
 * different <code>Statement</code> objects. All execution methods in the
 * <code>Statement</code> interface implicitly close a statment's current 
 * <code>ResultSet</code> object if an open one exists.
 *
 * @see Connection#createStatement
 * @see ResultSet 
 */

答案 1 :(得分:0)

换句话说,我相信Scorpion所说的是你需要一个新的rs1语句。尝试添加这样的新语句:

connection = dataSource.getConnection();
ResultSet rs;
connection.setAutoCommit(false);
System.out.println("Connected to server OELDBSQL!!!");
Statement stmt = connection.createStatement();

String querystring = "select empid,empname from empt";

rs = stmt.executeQuery(querystring);
Element child1 = doc.createElement("employee");
try {
    while (rs.next()) {
        child1.setAttributeNS(emp, "emp:empid", rs.getString(1));
        child1.setAttributeNS(emp, "emp:empname", rs.getString(2));
    }
        String querystring1 = "select locname from Locate";
        Statement stmt1 = connection.createStatement();
        ResultSet rs1;
        rs1 = stmt1.executeQuery(querystring1);
        while (rs1.next()) {
            Element element = doc.createElement("location");
            child1.appendChild(element);

            element.setAttributeNS(loc, "loc:locaname", rs.getString(1));
        }
    } catch (Exception e) {
        System.out.println("Exception in connecting to DB"
                + e.getMessage());
        System.err.println(e.getMessage());
    }
} catch (Exception e) {
    System.out
            .println("Exception in connecting to DB" + e.getMessage());
    System.err.println(e.getMessage());
}

此外,我注意到您没有关闭结果集/语句/连接。我强烈建议您关闭它们(按照创建的相反顺序)。