java.sql.SQLException: 流已被关闭

rel=nofollow target=_blank2019独角兽企业重金招聘Python工程师标准 数据库字段如下: ID INTEGER USERNAME VARCHAR2(50) Y SENDER...

>> ' rel='nofollow' target='_blank'>2019独角兽企业重金招聘Python工程师标准>>>

数据库字段如下:
ID       INTEGER                                
USERNAME VARCHAR2(50) Y                         
SENDER   VARCHAR2(50) Y                         
MESSAGE  LONG         Y                         
SENDTIME VARCHAR2(20) Y                         
ISREAD   VARCHAR2(1)  Y     

DAO 层的getAll方法
public List<SmsPo> getAll()
{
String listSql="select * from sms";
List<SmsPo> listSms=new ArrayList<SmsPo>();
PreparedStatement ps=DbBase.prepare(connection, listSql);
try {
ResultSet rs=ps.executeQuery();
while(rs.next())
{
SmsPo smsPo=new SmsPo();
smsPo.setId(rs.getInt(1));
smsPo.setUsername(rs.getString("username"));
smsPo.setSender(rs.getString("sender"));
smsPo.setSendtime(rs.getString("sendtime"));
smsPo.setMessage(rs.getString("message"));
smsPo.setIsRead(rs.getBoolean("isread"));
listSms.add(smsPo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DbBase.close(ps);
DbBase.close(connection);
}
return listSms;
}

main函数的测试:
public static void main(String[] args){
SmsService smsService=new SmsService();
List<SmsPo> list=smsService.getAll();
Iterator<SmsPo> iterator=list.iterator();
while(iterator.hasNext())
{
SmsPo smsPo=iterator.next();
System.out.println(smsPo.getUsername());
}
}
 此时运行,会出现错误:
java.sql.SQLException: 流已被关闭
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:207)
at oracle.jdbc.driver.LongAccessor.getBytes(LongAccessor.java:150)
at oracle.jdbc.driver.LongAccessor.getString(LongAccessor.java:192)
at oracle.jdbc.driver.T4CLongAccessor.getString(T4CLongAccessor.java:421)
at oracle.jdbc.driver.OracleResultSetImpl.getString(OracleResultSetImpl.java:396)
at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:1514)
at com.yy180.docman.dao.SmsDAO.getAll(SmsDAO.java:33)
at com.yy180.docman.service.SmsService.getAll(SmsService.java:19)
at com.yy180.all.TestAll.main(TestAll.java:12)


而当我将DAO层的相关代码修改一下顺序:
while(rs.next())
{
SmsPo smsPo=new SmsPo();
smsPo.setId(rs.getInt(1));
smsPo.setUsername(rs.getString("username"));
smsPo.setSender(rs.getString("sender"));
smsPo.setMessage(rs.getString("message"));
smsPo.setSendtime(rs.getString("sendtime"));
smsPo.setIsRead(rs.getBoolean("isread"));
listSms.add(smsPo);
}

运行一下就可以了。。
原因可能是读取数据库的流是按顺序的,当不按顺序读取时,就会出现错误,

转载于:https://my.oschina.net/u/2552902/blog/543982

本文标题为:java.sql.SQLException: 流已被关闭

基础教程推荐