[記錄]數據庫出錯解決

1. Operation not allowed after ResultSet closed

自己重構項目優化代碼,在數據庫工具類DBUtil類中關閉了Connection和PrepareStatement,再使用ResultSet對象賦值return傳遞結果在另一個文件接收ResultSet結果,系統報錯如題。搜索後瞭解到

Closing aStatement object will close and invalidate any instances of ResultSet producedby that Statement object. The resources held by the ResultSet object may not bereleased until garbage collection runs again, so it is a good practice to explicitlyclose ResultSet objects when they are no longer needed.

關閉statement對象將關閉該語句對象生成的ResultSet的任何實例並使其失效。在垃圾回收再次運行之前,resultset對象所持有的資源可能不會重新分配,因此當不再需要resultset對象時,最好明確地循環這些對象。
因此,如果想讓Preparestatement執行結果的ResultSet不失效,就不能先關閉PrepareStatement。需要在提取完ResultSet結果值再關閉。

如果,覺得文章對你有用,右側邊欄給個打賞吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章