报错提示:java.sql.SQLException: After end of result set

public class SortAndZip {
	public static void main() {
		String targetTable1 = "targetTable1 ";
		String targetTable2 = "targetTable2 ";
		Connection conn = null;
		PreparedStatement typePs = null;
		PreparedStatement postPs = null;
		ResultSet postRs = null;
		ResultSet typeRs = null;
		try {
			conn = JDBCUtils.getConnection();
			String[] keyStr = getKeyStr();
			String postSql = " select  " + keyStr[0] + " from " + targetTable1 + " where code = " + "'111'";
			String typeSql = " select  " + keyStr[1] + " from " + targetTable2 + " where code = " + "'111'";
			typePs = conn.prepareStatement(typeSql);
			postPs = conn.prepareStatement(postSql);
			postRs = postPs.executeQuery();
			typeRs = typePs.executeQuery();
			String[] postKey = keyStr[0].split(",");
			String[] typeKey = keyStr[1].split(",");
			while (postRs.next()) {
				ArrayList postStr = new ArrayList<String>();
				for (String k : postKey) {
					postStr.add(postRs.getString(k));
				}
				System.out.println(StringUtils.join(postStr.toArray(), ","));
			}
			while (typeRs.next()) {
				ArrayList typeStr = new ArrayList<String>();
				for (String k : typeKey) {
					typeStr.add(postRs.getString(k));
				}
				System.out.println(StringUtils.join(typeStr.toArray(), ","));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JDBCUtils.closeResultSetResource(postRs, typeRs);
			JDBCUtils.closeStatementResource(postPs, typePs);
			JDBCUtils.closeConnResource(conn);
		}
	}

倒数第12行(typeStr.add(postRs.getString(k)))正常情况下应该用typeStr.add(typeRs.getString(k)),正是重复用了postRs导致报错。

原因分析:
一个Statement对象同时只能有一个结果集在活动,就是说即使没有调用ResultSet的close()方法,只要打开第二个结果集就隐含着对上一个结果集的关闭,而上方代码中postRs用了两次,postRs第一次用后,再次调用postRs时,postRs就会关闭,就会报错;在JDBC3.0中,可以配置相关参数使其不关闭;而且ResultSet存储查询结果,ResultSet只能读取一次,不能够来回的滚动读取,当然也可以创建可来回滚动ResultSet

注:
JDBCUtils.closeResultSetResource(postRs, typeRs);
JDBCUtils.closeStatementResource(postPs, typePs);
JDBCUtils.closeConnResource(conn);
getKeyStr() ;这些是我自定义的一些类和方法

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魏一鸣*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值