我有以下类:
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…
关闭连接,从而关闭ResultSet。
与其将ResultSet存储在类成员中,不如将其存储在Refunds
内部的局部变量中,并在从Refunds
方法返回并关闭连接之前从中读取所有数据。