国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Java資源網 一個stmt多個rs進行操作引起的ResultSet已經關閉錯誤
一個stmt多個rs進行操作引起的ResultSet已經關閉錯誤
2005-08-09   來源:CSDN  作者:yizdream
一個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();//由于執(zhí)行了rst=stmt.executeQuery(sql_a);rs就會被關閉掉!所以程序執(zhí)行到此會提示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.就多創(chuàng)建幾個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();
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
用JAVA從數據庫中讀出字段及內容
請教JDBC怎么連接ORACLE數據庫
JSP連接各類數據庫大全(上)
jsp與數據庫連接大全
jdbc 連接 數據庫寫法
循環(huán)查詢 以及PreparedStatement
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服