SQLExecption:Operation not allowed after ResultSet closed解決辦法

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

原網址:http://blog.csdn.net/sku0923/article/details/1722370

一個stmt多個rs進行操作引起的ResultSet已經關閉錯誤

一個stmt多個rs進行操作. 那麼從stmt得到的rs1,必須馬上操作此rs1後,才能去得到另外的rs2,再對rs2操作. 不能互相交替使用,會引起rs已經關閉錯誤. 錯誤的代碼如下:  stmt=conn.createStatement();  

rs=stmt.executeQuery("select * from t1");  

rst=stmt.executeQuery("select * from t2");

rst.last();

 rs.last();//由於執行了rst=stmt.executeQuery(sql_a);rs就會被關閉掉!所以程式執行到此會提示ResultSet

已經關閉.錯誤資訊為:java.sql.SQLException: Operation not allowed fter ResultSet closed  

正確的代碼:
 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();

SQLExecption:Operation not allowed after ResultSet closed解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.