怎么写代码不需要手动关闭连接?

try{
    if(result != null){
        result.close();
        result = null;
    }  
}catch(SQLException e){
    e.printStackTrace();
}finally{
    try{
        if(ps != null){
            ps.close();
     ps = null;
   }
    }catch(SQLException e){
        e.printStackTrace();
    }finally{
        try{
     if(conn != null){
   conn.close();
   conn = null;
     }
}catch(SQLException e){
     e.printStackTrace();
}finally{
     return "数据库连接已关闭";
}
    }
}

posted @ 2019-09-05 10:52  梦人亭  阅读(156)  评论(0编辑  收藏  举报