我有以下课程:
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 ......
答案 0 :(得分:3)
关闭连接,关闭ResultSet。
不是将ResultSet存储在类成员中,而是将其存储在Refunds
内的局部变量中,并在从Refunds
方法返回并关闭连接之前从中读取所有数据。