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

后端 未结 7 1764
盖世英雄少女心
盖世英雄少女心 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:59

    Both ways works, but I prefer the first rs.close(); , without forgeting to check if rs is not null and to enclose your call by a try..catch statement so you have to close your connection in the finaly block even if Java7 close all for you.

    check Java / JDBC: Best design pattern to close database connection when exception occurs

提交回复
热议问题