• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

一个stmt多个rs进行操作引起的ResultSe

servlet/jsp 搞代码 7年前 (2018-06-18) 269次浏览 已收录 0个评论

一个stmt多个rs进行操作.那么从stmt得到的rs1,必须马上操作此rs1后,才能去得到另外的rs2,再对rs2操作.不能互相交替使用,会引起rs已经关闭错误.错误的代码如下: stmt=conn.createStatement(); rs=stmt.executeQuery("select * from t1"); rst=stmt.executeQuery("select * from t2"); rs.last();//由于执行了rst=stmt.executeQuery(sql_a);rs就会被关闭掉!所以程序执行到此会提示ResultSet已经关闭.错误信息为:java.sql.SQLException: Operation not allowed after ResultSet closed rst.last();正确的代码: stmt=conn.createStatement(); rs=stmt.executeQuery("select * from t1"); rs.last();//对rs的操作应马上操作,操作完后再从数据库得到rst,再对rst操作 rst=stmt.executeQuery("select * from t2"); rst.last();原因是: The  object  used  for  executing  a  static  SQL  statement  and  returning  the  results  it  produces.     By  default,  only  one  ResultSet  object  per  Statement  object  can  be  open  at  the  same  time.  Therefore,  if  the  reading  of  one  ResultSet  object  is  interleaved  with  the  reading  of  another,  each  must  have  been  generated  by  different  Statement  objects.  All  execution  methods  in  the  Statement  interface  implicitly  close  a  statment’s  current  ResultSet  object  if  an  open  one  exists.     一个stmt最好对应一个rs, 如果用一个时间内用一个stmt打开两个rs同时操作,会出现这种情况.所以解决此类问题:1.就多创建几个stmt,一个stmt对应一个rs;2.若用一个stmt对应多个rs的话,那只能得到一个rs后就操作,处理完第一个rs后再处理其他的,如上"正确代码".多个stmt对应各自的rs.stmt=conn.createStatement();stmt2=conn.createStatement();rs=stmt.executeQuery("select * from t1");rst=stmt2.executeQuery("select * from t2");rs.last();rst.last();

欢迎大家阅读《一个stmt多个rs进行操作引起的ResultSe》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:一个stmt多个rs进行操作引起的ResultSe
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址