2014-11-03 42 views
0

我有以下類:當我從我的方法返回它在我的ResultSet常閉

public class Refunds { 
    ResultSet dataToHash = null; 

    public Refunds (String UrnId) { 
     Database db = null; 
     CallableStatement callable; 
     String query = "select * from testmdb.dbo.ApEdiZcusSaSendFile where SourceID='LAN' and UrnID=?";  

     // Get database connection 
     try { 
      db = new Database("jdbc/refund"); 
     } catch (NamingException | SQLException e1) { 
      e1.printStackTrace(); 
     }       

     // Run the query 
     try { 
      callable = db.connection.prepareCall(query); 
      callable.setString(1, UrnId); 
      dataToHash = callable.executeQuery();    

     } catch (SQLException s) { 
      System.out.println("A SQL exception was thrown while running the query: "); 
      s.printStackTrace(); 
     } catch (Exception e) { 
      System.out.println("A general exception was thrown while running the query: "); 
      e.printStackTrace(); 
     } finally { 
      db.closeConnection(); 
     }     
    } 

    public ResultSet getDataToHash() { 
     return dataToHash; 
    } 
} 

我用它是這樣的:

// Get the result set 
Refunds refunds = new Refunds(urnId); 
ResultSet dataToHash = refunds.getDataToHash(); 

然而,每一次dataToHash.closed() 。我不關閉我的ResultSet。無論問題出在哪裏,我該如何修改這些代碼,以便在我得到它時不會被關閉?

PS - 不理會我的老同學System.outs ...

回答

3

您關閉連接,而關閉的ResultSet。

不是將ResultSet存儲在類成員中,而是將其存儲在Refunds中的局部變量中,並在從Refunds方法返回並關閉連接之前從中讀取所有數據。

+0

在某個階段,我必須關閉連接。我將在那裏移動它? – Tiwaz89 2014-11-03 09:00:36

+0

@Relborg根本不返回ResultSet;從中提取你需要的東西,把提取的數據放入適當的Collection中並返回。 – JonK 2014-11-03 09:01:23

+0

@JonK我完全同意你的看法,但是,我被指示不幸由我的高級開發人員返回ResultSet ... – Tiwaz89 2014-11-03 09:02:15

相關問題