2011-09-23 63 views
0

我想编写一个查询,该查询应该从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()); 
    } 
+0

我尝试没有尝试块使用单循环结果集。 – Sharada

+0

querystring =“从empt中选择empt.empid,empt.empname,locate.locname,找到;然后给出多个值表示假设empt表有5个名字,这5个名字用locname重复5次。行 – Sharada

回答

3

你已经打开用同样的语句,恕我直言,这行代码RS1后

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

将抛出你的例外,因为它正在对旧的结果集(RS)

Javadocs的Statement类陈述:

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

换句话说,我相信什么是Scorpion是sa ying是你需要一个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()); 
} 

另外我注意到你没有关闭你的resultsets/statements/connection。我强烈建议您关闭它们(按照创建的相反顺序)。