读取数据库时报java.sql.SQLException: 流已被关闭
来源:互联网 发布:淘宝卖家怎么快速升级 编辑:程序博客网 时间:2024/06/05 07:51
问题描述:
使用Connection、Statement/PreparedStatement、ResultSet来取数据库信息:
.........
ResultSet rs = stmt.executeQuery();
String str1 = rs.getString(field1);
......
String str2 = rs.getString(field2);
.........
当执行到String str2 = rs.getString(field2);时抛出异常:java.sql.SQLException: 流已被关闭
检查程序代码,缺未发现有什么异常的地方,那么到底是什么原因造成的呢?
出错原因及解决方法:
仔细查看java api文档,在ResultSet类的最上面有一段文字:
默认的 ResultSet
对象不可更新,仅有一个向前移动的指针。因此,只能迭代它一次,并且只能按从第一行到最后一行的顺序进行。
就是说ResultSet只能向前移动(rs.next()),而且只能按所取的记录的列的顺序来读取,所以这里:
String str1 = rs.getString(field1);
String str2 = rs.getString(field2);
如果所取的记录里字段field2在field1之前,而读取的时候读field2在读field1之后,那么就会抛出上述异常!!
解决方法非常简单:要么修改select语句使得field2在field1之前,要么修改读取语句的顺序!
- 读取数据库时报java.sql.SQLException: 流已被关闭
- java.sql.SQLException: 流已被关闭
- ,java.sql.SQLException:流已被关闭
- java.sql.SQLException: 关闭的连接
- java.sql.SQLException: 关闭的连接
- java.sql.SQLException: 关闭的连接
- java.sql.SQLException: 关闭的连接
- java.sql.SQLException: 关闭的 Resultset: next
- java.sql.SQLException: 关闭的连接 解决办法
- java中JDBCf访问数据库时报java.sql.SQLException: ORA-01017: invalid username/password; logon denied的错误
- 报错SQL state [null]; error code [17027]; 流已被关闭; nested exception is java.sql.SQLException: 流已被关闭
- 【已解决】java.sql.SQLException: 关闭的连接: next
- 从数据库中读取信息时抛出异常java.sql.SQLException: Parameter index out of range (1 > number of parameter
- java读取数据库中的错误:"Unhandled exception type SQLException"
- java连接mysql数据库,出现错误:java.sql.SQLException
- jdbc用ResultSet.relative分页时报错java.sql.SQLException: 没有当前行: relative
- java.sql.SQLException异常
- java.sql.SQLException
- "局"和"猪
- Tag区域大小
- 毕业离歌] 我拿什么拯救你,我的同学?
- web.xml
- common.tld
- 读取数据库时报java.sql.SQLException: 流已被关闭
- 键盘上每个键作用
- CommonMenu.java
- SetParamJS.java
- C编译基础
- vc模拟鼠标键盘操作实用类
- Tag打印功能
- index.jsp
- MySQL配置文件中文详尽解说版