2011-09-23 62 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。我強烈建議您關閉它們(按照創建的相反順序)。