What is the suitable way to close the database connection in Java?

后端 未结 7 1763
盖世英雄少女心
盖世英雄少女心 2020-12-20 04:15

I tried to close the DB connection.But had a bit of confusion, say

ResultSet rs = null 

Whether I have to it close by

rs.c         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 04:58

    I think the best way is do everything within a try-with-resources

    try(conn=openConnection()){
      try(rs=conn.getResultSet()){
      }
    }
    

    So that you be entirely sure the resources will be properly closed at the end.

提交回复
热议问题