gpt4 book ai didi

java - "The result set is closed"嵌套执行查询出错

转载 作者:行者123 更新时间:2023-11-29 06:14:16 24 4
gpt4 key购买 nike

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

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

下面的代码给出了错误

"The result set is closed".

如何解决这个问题?

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

最佳答案

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

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

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

Statement 类的 Javadoc 指出:

/**
* <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
*/

关于java - "The result set is closed"嵌套执行查询出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7524735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com