gpt4 book ai didi

java - 收到错误 SQLServerException : The result set is closed

转载 作者:行者123 更新时间:2023-12-02 10:20:08 26 4
gpt4 key购买 nike

我尝试了很多方法,但总是遇到此错误。
实际上,我正在尝试从其他类访问结果集值,并且对于数据库查询,我创建了一个单独的类。

请不要按照之前的要求标记此问题,因为我只得到了单个类的解决方案。
这是我的 DBVerification 类

    public class DBVerification {
private static String DB_URL = PropertyManager.getInstance().getDB_URL();
private static String DB_USER= PropertyManager.getInstance().getDB_USER();
private static String DB_PASSWORD= PropertyManager.getInstance().getDB_PASSWORD();
private static String DBClass= PropertyManager.getInstance().getDBClass();
private static Connection connection;
public static ArrayList<ResultSet> executeStoredProcedure(String query) throws ClassNotFoundException, SQLException
{
ArrayList<ResultSet> resultset = new ArrayList<ResultSet>();
Class.forName(DBClass);
connection= DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
CallableStatement cstmt = connection.prepareCall( "{ call " + query+" }" );
//System.out.println("{ call " + query+" }");
try {
boolean results = cstmt.execute();
int rsCount = 0;
do {
if(results) {
ResultSet rs = cstmt.getResultSet();
resultset.add(rs);
rsCount++;
System.out.println("RESULT SET #" + rsCount);
// rs.close();
}
System.out.println();
results = cstmt.getMoreResults();
} while(results);
//cstmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
//cstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
return resultset;
}
public static void closeDB() throws SQLException
{
connection.close();
}
}

这是我的第二节课报销课

public class Reimbursement
{
ArrayList<ResultSet> result = DBVerification.executeStoredProcedure("getreimbursements");
for (ResultSet curInstance: result) {
if(result.indexOf(curInstance) == 0)
{
while(curInstance.next())
{
String branchName=curInstance.getString("BranchName");
String department=curInstance.getString("DepartmentName");
String employee=curInstance.getString("EmployeeName");
String title=curInstance.getString("Title");
String claimdate=ValueConverter.DateFormat(curInstance.getString("Date"));


}
curInstance.close();

}
if(result.indexOf(curInstance) == 1)
{
while(curInstance.next())
{
String category=curInstance.getString("Category");
String expensedate=ValueConverter.DateFormat(curInstance.getString("ExpenseDate"));
String description=curInstance.getString("Description");
String approvedby=curInstance.getString("ApprovedBy");


}
curInstance.close();

}
}
DBVerification.closeDB();
}

请不要寻找 main 方法,因为这是用于测试类的,所以我已经在我的 xml 文件中使用了这个类。
请给我建议,告诉我我做错了什么,它给我错误消息“结果集已关闭”。

异常图像实际上是我的测试类,因此它只会以这种形式显示错误,我已经编辑了指示的行
exception message

最佳答案

认为您的问题可能如下

  • 循环存储过程调用的响应,将调用中的每个结果集添加到数组列表中
  • 将数组列表返回给调用方法并对其进行迭代
  • 您尝试依次处理每个结果集。

不幸的是,我认为 cstmt.getMoreResults() 的操作会在移至下一个结果集之前关闭所有打开的结果集。您最终得到的是一个封闭 ResultSet 对象的数组列表。当您尝试读取它们时,您会收到错误消息“结果集已关闭”

来自java文档

boolean getMoreResults() throws SQLException

Moves to this Statement object's next result, returns true if it is a ResultSet object, and implicitly closes any current ResultSet object(s) obtained with the method getResultSet.

关于java - 收到错误 SQLServerException : The result set is closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54401878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com