当前位置: 代码迷 >> Eclipse >> ResultSet is closed解决办法
  详细解决方案

ResultSet is closed解决办法

热度:240   发布时间:2016-04-23 11:28:54.0
ResultSet is closed
String sql1="select tlongitude,tlatitude,type,name,status,dlongitude,dlatitude,value,time,tname,opendegree,deviceid from (((tubulation as a left join device as b on a.ID=b.tubulationID) left join realTimeData as c on b.ID=c.deviceid) left join dataType as d on d.id=c.Did) left join controldata as e on e.cddeviceid=b.ID";

try {
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery(sql1);
while(rs.next()){
String sql2="select top 1 time from realTimeData where deviceid="+rs.getInt(12)+" order by time desc";
ResultSet rs2=stmt.executeQuery(sql2);
while(rs2.next()){


System.out.println(rs2.getString(1));
}
}


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
错误提示:
java.sql.SQLException: ResultSet is closed
at sun.jdbc.odbc.JdbcOdbcResultSet.checkOpen(JdbcOdbcResultSet.java:6646)
at sun.jdbc.odbc.JdbcOdbcResultSet.next(JdbcOdbcResultSet.java:1249)
at com.jointsky.handle.Hello.main(Hello.java:45)


------解决方案--------------------
一个stmt最好对应一个rs, 如果用一个时间内用一个stmt打开两个rs同时操作,会出现这种情况.
所以解决此类问题:1.就多创建几个stmt,一个stmt对应一个rs;2.若用一个stmt对应多个rs的话,那只能得到一个rs后就操作,处理完第一个rs后再处理其他的,如上"正确代码".

多个stmt对应各自的rs.
stmt1=conn.createStatement();
stmt2=conn.createStatement();
rs1=stmt.executeQuery("select * from t1");
rs2=stmt2.executeQuery("select * from t2");
rs1.last();
rs2.last();
这样就可以解决在相同页面中同一smrt下只能使用一个结果集的问题!

  相关解决方案