[记录]数据库出错解决

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结果值再关闭。

如果,觉得文章对你有用,右侧边栏给个打赏吧!

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