How to properly clean up JDBC resources in Java?

后端 未结 9 1963
说谎
说谎 2020-12-16 03:17

What is considered best practices when cleaning up JDBC resources and why? I kept the example short, thus just the cleaning up of the ResultSet.

finally
{
          


        
9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 03:34

    protected void closeAll(){
            closeResultSet();
            closeStatement();
            closeConnection();
        }   
    
    protected void closeConnection(){
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    /*Logger*/
                }
                connection = null;
            }
        }
    
    
        protected void closeStatement() {
            if (stmt != null) {
                try {
                    ocstmt.close();
                } catch (SQLException e) {
                    /*Logger*/
                }
                ocstmt = null;
            }
        }
    
    
        protected void closeResultSet() {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    /*Logger*/
                }
                rs = null;
            }
        }
    

提交回复
热议问题