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 ...
在某個階段,我必須關閉連接。我將在那裏移動它? – Tiwaz89 2014-11-03 09:00:36
@Relborg根本不返回ResultSet;從中提取你需要的東西,把提取的數據放入適當的Collection中並返回。 – JonK 2014-11-03 09:01:23
@JonK我完全同意你的看法,但是,我被指示不幸由我的高級開發人員返回ResultSet ... – Tiwaz89 2014-11-03 09:02:15